github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/nup_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 "path/filepath" 21 "testing" 22 23 "github.com/pdfcpu/pdfcpu/pkg/api" 24 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" 25 ) 26 27 func testNUp(t *testing.T, msg string, inFiles []string, outFile string, selectedPages []string, desc string, n int, isImg bool, conf *model.Configuration) { 28 t.Helper() 29 30 var ( 31 nup *model.NUp 32 err error 33 ) 34 35 if isImg { 36 if nup, err = api.ImageNUpConfig(n, desc, conf); err != nil { 37 t.Fatalf("%s %s: %v\n", msg, outFile, err) 38 } 39 } else { 40 if nup, err = api.PDFNUpConfig(n, desc, conf); err != nil { 41 t.Fatalf("%s %s: %v\n", msg, outFile, err) 42 } 43 } 44 45 if err := api.NUpFile(inFiles, outFile, selectedPages, nup, conf); err != nil { 46 t.Fatalf("%s %s: %v\n", msg, outFile, err) 47 } 48 if err := api.ValidateFile(outFile, conf); err != nil { 49 t.Fatalf("%s: %v\n", msg, err) 50 } 51 } 52 53 func TestNUp(t *testing.T) { 54 55 outDir := filepath.Join(samplesDir, "nup") 56 57 for _, tt := range []struct { 58 msg string 59 inFiles []string 60 outFile string 61 selectedPages []string 62 desc string 63 unit string 64 n int 65 isImg bool 66 }{ 67 // 4-Up a PDF 68 {"TestNUpFromPDF", 69 []string{filepath.Join(inDir, "WaldenFull.pdf")}, 70 filepath.Join(outDir, "NUpFromPDF.pdf"), 71 nil, 72 "dim: 400 800, margin:10, bgcol:#f7e6c7", 73 "mm", 74 9, 75 false}, 76 77 // 2-Up a PDF with CropBox 78 {"TestNUpFromPdfWithCropBox", 79 []string{filepath.Join(inDir, "grid_example.pdf")}, 80 filepath.Join(outDir, "NUpFromPDFWithCropBox.pdf"), 81 nil, 82 "form:A5L, border:on, margin:0, bgcol:#f7e6c7", 83 "points", 84 2, 85 false}, 86 87 // 16-Up an image 88 {"TestNUpFromSingleImage", 89 []string{filepath.Join(resDir, "logoSmall.png")}, 90 filepath.Join(outDir, "NUpFromSingleImage.pdf"), 91 nil, 92 "form:A3P, ma:10, bgcol:#f7e6c7", 93 "points", 94 16, 95 true}, 96 97 // 6-Up a sequence of images. 98 {"TestNUpFromImages", 99 imageFileNames(t, resDir), 100 filepath.Join(outDir, "NUpFromImages.pdf"), 101 nil, 102 "form:Tabloid, border:on, ma:10, bgcol:#f7e6c7", 103 "points", 104 6, 105 true}, 106 } { 107 conf := model.NewDefaultConfiguration() 108 conf.SetUnit(tt.unit) 109 testNUp(t, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.n, tt.isImg, conf) 110 } 111 }