github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/booklet_test.go (about) 1 /* 2 Copyright 2021 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/api" 24 "github.com/pdfcpu/pdfcpu/pkg/cli" 25 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" 26 ) 27 28 func testBooklet(t *testing.T, msg string, inFiles []string, outFile string, selectedPages []string, desc string, n int, isImg bool, conf *model.Configuration) { 29 t.Helper() 30 31 var ( 32 booklet *model.NUp 33 err error 34 ) 35 36 if isImg { 37 if booklet, err = api.ImageBookletConfig(n, desc, conf); err != nil { 38 t.Fatalf("%s %s: %v\n", msg, outFile, err) 39 } 40 } else { 41 if booklet, err = api.PDFBookletConfig(n, desc, conf); err != nil { 42 t.Fatalf("%s %s: %v\n", msg, outFile, err) 43 } 44 } 45 46 cmd := cli.BookletCommand(inFiles, outFile, selectedPages, booklet, conf) 47 if _, err := cli.Process(cmd); err != nil { 48 t.Fatalf("%s %s: %v\n", msg, outFile, err) 49 } 50 51 if err := validateFile(t, outFile, conf); err != nil { 52 t.Fatalf("%s: %v\n", msg, err) 53 } 54 } 55 56 func TestBookletCommand(t *testing.T) { 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 68 // 2-up booklet from images on A4 69 {"TestBookletFromImagesA42Up", 70 imageFileNames(t, resDir), 71 filepath.Join(outDir, "BookletFromImagesA4_2Up.pdf"), 72 nil, 73 "p:A4, border:false, g:on, ma:25, bgcol:#beded9", 74 "points", 75 2, 76 true, 77 }, 78 79 // 4-up booklet from images on A4 80 {"TestBookletFromImagesA44Up", 81 imageFileNames(t, resDir), 82 filepath.Join(outDir, "BookletFromImagesA4_4Up.pdf"), 83 nil, 84 "p:A4, border:false, g:on, ma:25, bgcol:#beded9", 85 "points", 86 4, 87 true, 88 }, 89 90 // 2-up booklet from PDF on A4 91 {"TestBookletFromPDF2UpA4", 92 []string{filepath.Join(inDir, "zineTest.pdf")}, 93 filepath.Join(outDir, "BookletFromPDFA4_2Up.pdf"), 94 nil, // all pages 95 "p:A4, border:false, g:on, ma:10, bgcol:#beded9", 96 "points", 97 2, 98 false, 99 }, 100 101 // 4-up booklet from PDF on A4 102 {"TestBookletFromPDF4UpA4", 103 []string{filepath.Join(inDir, "zineTest.pdf")}, 104 filepath.Join(outDir, "BookletFromPDFA4_4Up.pdf"), 105 []string{"1-"}, // all pages 106 "p:A4, border:off, guides:on, ma:10, bgcol:#beded9", 107 "points", 108 4, 109 false, 110 }, 111 112 // 4-up booklet from PDF on Ledger 113 {"TestBookletFromPDF4UpLedger", 114 []string{filepath.Join(inDir, "bookletTest.pdf")}, 115 filepath.Join(outDir, "BookletFromPDFLedger_4Up.pdf"), 116 []string{"1-24"}, 117 "p:LedgerP, g:on, ma:10, bgcol:#f7e6c7", 118 "points", 119 4, 120 false, 121 }, 122 123 // 4-up booklet from PDF on Ledger where the number of pages don't fill the whole sheet 124 {"TestBookletFromPDF4UpLedgerWithTrailingBlankPages", 125 []string{filepath.Join(inDir, "bookletTest.pdf")}, 126 filepath.Join(outDir, "BookletFromPDFLedger_4UpWithTrailingBlankPages.pdf"), 127 []string{"1-21"}, 128 "p:LedgerP, g:on, ma:10, bgcol:#f7e6c7", 129 "points", 130 4, 131 false, 132 }, 133 134 // 2-up booklet from PDF on Letter 135 {"TestBookletFromPDF2UpLetter", 136 []string{filepath.Join(inDir, "bookletTest.pdf")}, 137 filepath.Join(outDir, "BookletFromPDFLetter_2Up.pdf"), 138 []string{"1-16"}, 139 "p:LetterP, g:on, ma:10, bgcol:#f7e6c7", 140 "points", 141 2, 142 false, 143 }, 144 145 // 2-up booklet from PDF on Letter where the number of pages don't fill the whole sheet 146 {"TestBookletFromPDF2UpLetterWithTrailingBlankPages", 147 []string{filepath.Join(inDir, "bookletTest.pdf")}, 148 filepath.Join(outDir, "BookletFromPDFLetter_2UpWithTrailingBlankPages.pdf"), 149 []string{"1-14"}, 150 "p:LetterP, g:on, ma:10, bgcol:#f7e6c7", 151 "points", 152 2, 153 false, 154 }, 155 156 // 2-up multi folio booklet from PDF on A4 using 8 sheets per folio 157 // using the default foliosize:8 158 // Here we print 2 complete folios (2 x 8 sheets) + 1 partial folio 159 // multi folio only makes sense for n = 2 160 // See also https://www.instructables.com/How-to-bind-your-own-Hardback-Book/ 161 {"TestHardbackBookFromPDF", 162 []string{filepath.Join(inDir, "WaldenFull.pdf")}, 163 filepath.Join(outDir, "HardbackBookFromPDF.pdf"), 164 []string{"1-70"}, 165 "p:A4, multifolio:on, border:off, g:on, ma:10, bgcol:#beded9", 166 "points", 167 2, 168 false, 169 }, 170 } { 171 conf := model.NewDefaultConfiguration() 172 conf.SetUnit(tt.unit) 173 testBooklet(t, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.n, tt.isImg, conf) 174 } 175 }