github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/merge_test.go (about)

     1  /*
     2  Copyright 2020 The pdfcpu Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package test
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/pdfcpu/pdfcpu/pkg/cli"
    24  )
    25  
    26  // Merge all PDFs in testdir into out/test.pdf.
    27  func TestMergeCreateCommand(t *testing.T) {
    28  	msg := "TestMergeCreateCommand"
    29  
    30  	var inFiles []string
    31  	for _, f := range allPDFs(t, inDir) {
    32  		inFiles = append(inFiles, filepath.Join(inDir, f))
    33  	}
    34  
    35  	outFile := filepath.Join(outDir, "test.pdf")
    36  
    37  	cmd := cli.MergeCreateCommand(inFiles, outFile, true, conf)
    38  	if _, err := cli.Process(cmd); err != nil {
    39  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    40  	}
    41  
    42  	if err := validateFile(t, outFile, conf); err != nil {
    43  		t.Fatalf("%s: %v\n", msg, err)
    44  	}
    45  }
    46  
    47  func TestMergeCreateZippedCommand(t *testing.T) {
    48  	msg := "TestMergeCreateZippedCommand"
    49  
    50  	// The actual usecase for this is the recombination of 2 PDF files representing even and odd pages of some PDF source.
    51  	// See #716
    52  	inFiles := []string{
    53  		filepath.Join(inDir, "Acroforms2.pdf"),
    54  		filepath.Join(inDir, "adobe_errata.pdf"),
    55  	}
    56  	outFile := filepath.Join(outDir, "out.pdf")
    57  
    58  	cmd := cli.MergeCreateZipCommand(inFiles, outFile, conf)
    59  	if _, err := cli.Process(cmd); err != nil {
    60  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    61  	}
    62  
    63  	if err := validateFile(t, outFile, conf); err != nil {
    64  		t.Fatalf("%s: %v\n", msg, err)
    65  	}
    66  }
    67  
    68  func TestMergeAppendCommand(t *testing.T) {
    69  	msg := "TestMergeAppendCommand"
    70  
    71  	var inFiles []string
    72  	for _, f := range allPDFs(t, inDir) {
    73  		if f == "test.pdf" {
    74  			continue
    75  		}
    76  		inFiles = append(inFiles, filepath.Join(inDir, f))
    77  	}
    78  
    79  	outFile := filepath.Join(outDir, "test.pdf")
    80  
    81  	if err := copyFile(t, filepath.Join(inDir, "test.pdf"), outFile); err != nil {
    82  		t.Fatalf("%s: %v\n", msg, err)
    83  	}
    84  
    85  	// Merge inFiles by concatenation in the order specified and write the result to outFile.
    86  	// If outFile already exists its content will be preserved and serves as the beginning of the merge result.
    87  	cmd := cli.MergeAppendCommand(inFiles, outFile, false, conf)
    88  	if _, err := cli.Process(cmd); err != nil {
    89  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    90  	}
    91  
    92  	if err := validateFile(t, outFile, conf); err != nil {
    93  		t.Fatalf("%s: %v\n", msg, err)
    94  	}
    95  }