github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/booklet_test.go (about) 1 /* 2 Copyright 2021 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 testBooklet(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 booklet *model.NUp 32 err error 33 ) 34 35 if isImg { 36 if booklet, err = api.ImageBookletConfig(n, desc, conf); err != nil { 37 t.Fatalf("%s %s: %v\n", msg, outFile, err) 38 } 39 } else { 40 if booklet, err = api.PDFBookletConfig(n, desc, conf); err != nil { 41 t.Fatalf("%s %s: %v\n", msg, outFile, err) 42 } 43 } 44 45 if err := api.BookletFile(inFiles, outFile, selectedPages, booklet, 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 TestBooklet(t *testing.T) { 54 outDir := filepath.Join("..", "..", "samples", "booklet") 55 56 for _, tt := range []struct { 57 msg string 58 inFiles []string 59 outFile string 60 selectedPages []string 61 desc string 62 unit string 63 n int 64 isImg bool 65 }{ 66 // 2-up booklet from images on A4 67 {"TestBookletFromImagesA42Up", 68 imageFileNames(t, resDir), 69 filepath.Join(outDir, "BookletFromImagesA4_2Up.pdf"), 70 nil, 71 "p:A4, border:false, g:on, ma:25, bgcol:#beded9", 72 "points", 73 2, 74 true, 75 }, 76 77 // 4-up booklet from images on A4 78 {"TestBookletFromImagesA44Up", 79 imageFileNames(t, resDir), 80 filepath.Join(outDir, "BookletFromImagesA4_4Up.pdf"), 81 nil, 82 "p:A4, border:false, g:on, ma:25, bgcol:#beded9", 83 "points", 84 4, 85 true, 86 }, 87 88 // 2-up booklet from PDF on A4 89 {"TestBookletFromPDF2UpA4", 90 []string{filepath.Join(inDir, "zineTest.pdf")}, 91 filepath.Join(outDir, "BookletFromPDFA4_2Up.pdf"), 92 nil, // all pages 93 "p:A4, border:false, g:on, ma:10, bgcol:#beded9", 94 "points", 95 2, 96 false, 97 }, 98 99 // 4-up booklet from PDF on A4 100 {"TestBookletFromPDF4UpA4", 101 []string{filepath.Join(inDir, "zineTest.pdf")}, 102 filepath.Join(outDir, "BookletFromPDFA4_4Up.pdf"), 103 []string{"1-"}, // all pages 104 "p:A4, border:off, guides:on, ma:10, bgcol:#beded9", 105 "points", 106 4, 107 false, 108 }, 109 110 // 4-up booklet from PDF on Ledger 111 {"TestBookletFromPDF4UpLedger", 112 []string{filepath.Join(inDir, "bookletTest.pdf")}, 113 filepath.Join(outDir, "BookletFromPDFLedger_4Up.pdf"), 114 []string{"1-24"}, 115 "p:LedgerP, g:on, ma:10, bgcol:#f7e6c7", 116 "points", 117 4, 118 false, 119 }, 120 121 // 4-up booklet from PDF on Ledger where the number of pages don't fill the whole sheet 122 {"TestBookletFromPDF4UpLedgerWithTrailingBlankPages", 123 []string{filepath.Join(inDir, "bookletTest.pdf")}, 124 filepath.Join(outDir, "BookletFromPDFLedger_4UpWithTrailingBlankPages.pdf"), 125 []string{"1-21"}, 126 "p:LedgerP, g:on, ma:10, bgcol:#f7e6c7", 127 "points", 128 4, 129 false, 130 }, 131 132 // 2-up booklet from PDF on Letter 133 {"TestBookletFromPDF2UpLetter", 134 []string{filepath.Join(inDir, "bookletTest.pdf")}, 135 filepath.Join(outDir, "BookletFromPDFLetter_2Up.pdf"), 136 []string{"1-16"}, 137 "p:LetterP, g:on, ma:10, bgcol:#f7e6c7", 138 "points", 139 2, 140 false, 141 }, 142 143 // 2-up booklet from PDF on Letter where the number of pages don't fill the whole sheet 144 {"TestBookletFromPDF2UpLetterWithTrailingBlankPages", 145 []string{filepath.Join(inDir, "bookletTest.pdf")}, 146 filepath.Join(outDir, "BookletFromPDFLetter_2UpWithTrailingBlankPages.pdf"), 147 []string{"1-14"}, 148 "p:LetterP, g:on, ma:10, bgcol:#f7e6c7", 149 "points", 150 2, 151 false, 152 }, 153 154 // more nup 155 {"TestBookletFromPDF_2up_perfectbound", 156 []string{filepath.Join(inDir, "bookletTest.pdf")}, 157 filepath.Join(outDir, "BookletFromPDFLetter_2Up_perfectbound.pdf"), 158 []string{"1-24"}, 159 "p:LetterP, g:on, btype:perfectbound, ma:10, bgcol:#f7e6c7", 160 "points", 161 2, 162 false, 163 }, 164 {"TestBookletFromPDF_6up", 165 []string{filepath.Join(inDir, "bookletTest.pdf")}, 166 filepath.Join(outDir, "BookletFromPDFLedger_6Up.pdf"), 167 []string{"1-24"}, 168 "p:LedgerP, g:on, ma:10, bgcol:#f7e6c7", 169 "points", 170 6, 171 false, 172 }, 173 174 // misc orientations and booklet types on 4-up 175 {"TestBookletFromPDF_4up_portrait_short", 176 []string{filepath.Join(inDir, "bookletTest.pdf")}, 177 filepath.Join(outDir, "BookletFromPDFLedger_4Up_portrait_short.pdf"), 178 []string{"1-24"}, 179 "p:LedgerP, g:on, binding:short, ma:10, bgcol:#f7e6c7", 180 "points", 181 4, 182 false, 183 }, 184 {"TestBookletFromPDF_4up_landscape_long", 185 []string{filepath.Join(inDir, "bookletTestLandscape.pdf")}, 186 filepath.Join(outDir, "BookletFromPDFLedger_4Up_landscape_long.pdf"), 187 []string{"1-24"}, 188 "p:LedgerL, g:on, ma:10, bgcol:#f7e6c7", 189 "points", 190 4, 191 false, 192 }, 193 {"TestBookletFromPDF_4up_landscape_short", 194 []string{filepath.Join(inDir, "bookletTestLandscape.pdf")}, 195 filepath.Join(outDir, "BookletFromPDFLedger_4Up_landscape_short.pdf"), 196 []string{"1-24"}, 197 "p:LedgerL, g:on, binding:short, ma:10, bgcol:#f7e6c7", 198 "points", 199 4, 200 false, 201 }, 202 {"TestBookletFromPDF_4up-portrait_long_advanced", 203 []string{filepath.Join(inDir, "bookletTest.pdf")}, 204 filepath.Join(outDir, "BookletFromPDFLedger_4Up_portrait_long_advanced.pdf"), 205 []string{"1-24"}, 206 "p:LedgerP, g:on, btype:bookletadvanced, ma:10, bgcol:#f7e6c7", 207 "points", 208 4, 209 false, 210 }, 211 {"TestBookletFromPDF_4up_landscape_short_advanced", 212 []string{filepath.Join(inDir, "bookletTestLandscape.pdf")}, 213 filepath.Join(outDir, "BookletFromPDFLedger_4Up_landscape_short_advanced.pdf"), 214 []string{"1-24"}, 215 "p:LedgerL, g:on, binding:short, btype:bookletadvanced, ma:10, bgcol:#f7e6c7", 216 "points", 217 4, 218 false, 219 }, 220 {"TestBookletFromPDF_4up_perfectbound", 221 []string{filepath.Join(inDir, "bookletTest.pdf")}, 222 filepath.Join(outDir, "BookletFromPDFLedger_4Up_perfectbound.pdf"), 223 []string{"1-24"}, 224 "p:LedgerP, g:on, btype:perfectbound, ma:10, bgcol:#f7e6c7", 225 "points", 226 4, 227 false, 228 }, 229 230 // 8up 231 {"TestBookletFromPDF8Up", 232 []string{filepath.Join(inDir, "bookletTestA6.pdf")}, 233 filepath.Join(outDir, "BookletFromPDF8Up.pdf"), 234 nil, 235 "p:A3, g:on, ma:10, bgcol:#f7e6c7", 236 "points", 237 8, 238 false, 239 }, 240 {"TestBookletFromPDF8UpPortraitShort", 241 []string{filepath.Join(inDir, "bookletTestA6.pdf")}, 242 filepath.Join(outDir, "BookletFromPDF8UpPortraitShort.pdf"), 243 nil, 244 "p:A3, binding:short, g:on, ma:10, bgcol:#f7e6c7", 245 "points", 246 8, 247 false, 248 }, 249 {"TestBookletFromPDF8UpLandscapeLong", 250 []string{filepath.Join(inDir, "bookletTestA6L.pdf")}, 251 filepath.Join(outDir, "BookletFromPDF8UpLandscapeLong.pdf"), 252 nil, 253 "p:A3, binding:long, g:on, ma:10, bgcol:#f7e6c7", 254 "points", 255 8, 256 false, 257 }, 258 {"TestBookletFromPDF8UpLandscapeShort", 259 []string{filepath.Join(inDir, "bookletTestA6L.pdf")}, 260 filepath.Join(outDir, "BookletFromPDF8UpLandscapeShort.pdf"), 261 nil, 262 "p:A3, binding:short, g:on, ma:10, bgcol:#f7e6c7", 263 "points", 264 8, 265 false, 266 }, 267 268 // 2-up multi folio booklet from PDF on A4 using 8 sheets per folio 269 // using the default foliosize:8 270 // Here we print 2 complete folios (2 x 8 sheets) + 1 partial folio 271 // See also https://www.instructables.com/How-to-bind-your-own-Hardback-Book/ 272 {"TestHardbackBookFromPDF", 273 []string{filepath.Join(inDir, "WaldenFull.pdf")}, 274 filepath.Join(outDir, "HardbackBookFromPDF.pdf"), 275 []string{"1-70"}, 276 "p:A4, multifolio:on, border:off, g:on, ma:10, bgcol:#beded9", 277 "points", 278 2, 279 false, 280 }, 281 } { 282 t.Run(tt.msg, func(subTest *testing.T) { 283 conf := model.NewDefaultConfiguration() 284 conf.SetUnit(tt.unit) 285 testBooklet(subTest, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.n, tt.isImg, conf) 286 }) 287 } 288 }