github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/merge_test.go (about) 1 /* 2 Copyright 2020 The pdf 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 "bytes" 21 "io" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/pdfcpu/pdfcpu/pkg/api" 27 ) 28 29 func TestMergeCreateNew(t *testing.T) { 30 msg := "TestMergeCreate" 31 inFiles := []string{ 32 filepath.Join(inDir, "Acroforms2.pdf"), 33 filepath.Join(inDir, "adobe_errata.pdf"), 34 } 35 36 // Merge inFiles by concatenation in the order specified and write the result to outFile. 37 // outFile will be overwritten. 38 39 // Bookmarks for the merged document will be created/preserved per default (see config.yaml) 40 41 outFile := filepath.Join(outDir, "out.pdf") 42 if err := api.MergeCreateFile(inFiles, outFile, false, nil); err != nil { 43 t.Fatalf("%s: %v\n", msg, err) 44 } 45 46 if err := api.ValidateFile(outFile, conf); err != nil { 47 t.Fatalf("%s: %v\n", msg, err) 48 } 49 50 // Insert an empty page between merged files. 51 outFile = filepath.Join(outDir, "outWithDivider.pdf") 52 dividerPage := true 53 if err := api.MergeCreateFile(inFiles, outFile, dividerPage, nil); err != nil { 54 t.Fatalf("%s: %v\n", msg, err) 55 } 56 57 if err := api.ValidateFile(outFile, conf); err != nil { 58 t.Fatalf("%s: %v\n", msg, err) 59 } 60 } 61 62 func TestMergeCreateZipped(t *testing.T) { 63 msg := "TestMergeCreateZipped" 64 65 // The actual usecase for this is the recombination of 2 PDF files representing even and odd pages of some PDF source. 66 // See #716 67 inFile1 := filepath.Join(inDir, "Acroforms2.pdf") 68 inFile2 := filepath.Join(inDir, "adobe_errata.pdf") 69 outFile := filepath.Join(outDir, "out.pdf") 70 71 if err := api.MergeCreateZipFile(inFile1, inFile2, outFile, nil); err != nil { 72 t.Fatalf("%s: %v\n", msg, err) 73 } 74 75 if err := api.ValidateFile(outFile, conf); err != nil { 76 t.Fatalf("%s: %v\n", msg, err) 77 } 78 } 79 80 func TestMergeAppendNew(t *testing.T) { 81 msg := "TestMergeAppend" 82 inFiles := []string{ 83 filepath.Join(inDir, "Acroforms2.pdf"), 84 filepath.Join(inDir, "adobe_errata.pdf"), 85 } 86 outFile := filepath.Join(outDir, "test.pdf") 87 if err := copyFile(t, filepath.Join(inDir, "test.pdf"), outFile); err != nil { 88 t.Fatalf("%s: %v\n", msg, err) 89 } 90 91 // Merge inFiles by concatenation in the order specified and write the result to outFile. 92 // If outFile already exists its content will be preserved and serves as the beginning of the merge result. 93 94 // Bookmarks for the merged document will be created/preserved per default (see config.yaml) 95 96 if err := api.MergeAppendFile(inFiles, outFile, false, nil); err != nil { 97 t.Fatalf("%s: %v\n", msg, err) 98 } 99 if err := api.ValidateFile(outFile, conf); err != nil { 100 t.Fatalf("%s: %v\n", msg, err) 101 } 102 103 anotherFile := filepath.Join(inDir, "testRot.pdf") 104 err := api.MergeAppendFile([]string{anotherFile}, outFile, false, nil) 105 if err != nil { 106 t.Fatalf("%s: %v\n", msg, err) 107 } 108 if err := api.ValidateFile(outFile, conf); err != nil { 109 t.Fatalf("%s: %v\n", msg, err) 110 } 111 } 112 113 func TestMergeToBufNew(t *testing.T) { 114 msg := "TestMergeToBuf" 115 inFiles := []string{ 116 filepath.Join(inDir, "Acroforms2.pdf"), 117 filepath.Join(inDir, "adobe_errata.pdf"), 118 } 119 outFile := filepath.Join(outDir, "test.pdf") 120 121 destFile := inFiles[0] 122 inFiles = inFiles[1:] 123 124 buf := &bytes.Buffer{} 125 if err := api.Merge(destFile, inFiles, buf, nil, false); err != nil { 126 t.Fatalf("%s: merge: %v\n", msg, err) 127 } 128 129 if err := os.WriteFile(outFile, buf.Bytes(), 0644); err != nil { 130 t.Fatalf("%s: write: %v\n", msg, err) 131 } 132 133 if err := api.ValidateFile(outFile, conf); err != nil { 134 t.Fatalf("%s: %v\n", msg, err) 135 } 136 } 137 138 func TestMergeRaw(t *testing.T) { 139 msg := "TestMergeRaw" 140 inFiles := []string{ 141 filepath.Join(inDir, "Acroforms2.pdf"), 142 filepath.Join(inDir, "adobe_errata.pdf"), 143 } 144 outFile := filepath.Join(outDir, "test.pdf") 145 146 var rsc []io.ReadSeeker = make([]io.ReadSeeker, 2) 147 148 f0, err := os.Open(inFiles[0]) 149 if err != nil { 150 t.Fatalf("%s: open file1: %v\n", msg, err) 151 } 152 defer f0.Close() 153 rsc[0] = f0 154 155 f1, err := os.Open(inFiles[1]) 156 if err != nil { 157 t.Fatalf("%s: open file2: %v\n", msg, err) 158 } 159 defer f1.Close() 160 rsc[1] = f1 161 162 buf := &bytes.Buffer{} 163 if err := api.MergeRaw(rsc, buf, false, nil); err != nil { 164 t.Fatalf("%s: merge: %v\n", msg, err) 165 } 166 167 if err := os.WriteFile(outFile, buf.Bytes(), 0644); err != nil { 168 t.Fatalf("%s: write: %v\n", msg, err) 169 } 170 171 if err := api.ValidateFile(outFile, conf); err != nil { 172 t.Fatalf("%s: %v\n", msg, err) 173 } 174 }