Skip to content

Commit

Permalink
Fix #739
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Nov 27, 2023
1 parent 9964328 commit f7d021c
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 115 deletions.
8 changes: 4 additions & 4 deletions pkg/api/example_test.go
Expand Up @@ -185,22 +185,22 @@ func ExampleImportImagesFile() {
func ExampleNUpFile() {

// 4-Up in.pdf and write result to out.pdf.
nup, _ := PDFNUpConfig(4, "")
nup, _ := PDFNUpConfig(4, "", nil)
inFiles := []string{"in.pdf"}
NUpFile(inFiles, "out.pdf", nil, nup, nil)

// 9-Up a sequence of images using format Tabloid w/o borders and no margins.
nup, _ = ImageNUpConfig(9, "f:Tabloid, b:off, m:0")
nup, _ = ImageNUpConfig(9, "f:Tabloid, b:off, m:0", nil)
inFiles = []string{"in1.png", "in2.jpg", "in3.tiff"}
NUpFile(inFiles, "out.pdf", nil, nup, nil)

// TestGridFromPDF
nup, _ = PDFGridConfig(1, 3, "f:LegalL")
nup, _ = PDFGridConfig(1, 3, "f:LegalL", nil)
inFiles = []string{"in.pdf"}
NUpFile(inFiles, "out.pdf", nil, nup, nil)

// TestGridFromImages
nup, _ = ImageGridConfig(4, 2, "d:500 500, m:20, b:off")
nup, _ = ImageGridConfig(4, 2, "d:500 500, m:20, b:off", nil)
inFiles = []string{"in1.png", "in2.jpg", "in3.tiff"}
NUpFile(inFiles, "out.pdf", nil, nup, nil)
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/api/nup.go
Expand Up @@ -28,33 +28,33 @@ import (
)

// PDFNUpConfig returns an NUp configuration for Nup-ing PDF files.
func PDFNUpConfig(val int, desc string) (*model.NUp, error) {
return pdfcpu.PDFNUpConfig(val, desc)
func PDFNUpConfig(val int, desc string, conf *model.Configuration) (*model.NUp, error) {
return pdfcpu.PDFNUpConfig(val, desc, conf)
}

// ImageNUpConfig returns an NUp configuration for Nup-ing image files.
func ImageNUpConfig(val int, desc string) (*model.NUp, error) {
return pdfcpu.ImageNUpConfig(val, desc)
func ImageNUpConfig(val int, desc string, conf *model.Configuration) (*model.NUp, error) {
return pdfcpu.ImageNUpConfig(val, desc, conf)
}

// PDFGridConfig returns a grid configuration for Grid-ing PDF files.
func PDFGridConfig(rows, cols int, desc string) (*model.NUp, error) {
return pdfcpu.PDFGridConfig(rows, cols, desc)
func PDFGridConfig(rows, cols int, desc string, conf *model.Configuration) (*model.NUp, error) {
return pdfcpu.PDFGridConfig(rows, cols, desc, conf)
}

// ImageGridConfig returns a grid configuration for Grid-ing image files.
func ImageGridConfig(rows, cols int, desc string) (*model.NUp, error) {
return pdfcpu.ImageGridConfig(rows, cols, desc)
func ImageGridConfig(rows, cols int, desc string, conf *model.Configuration) (*model.NUp, error) {
return pdfcpu.ImageGridConfig(rows, cols, desc, conf)
}

// PDFBookletConfig returns an NUp configuration for Booklet-ing PDF files.
func PDFBookletConfig(val int, desc string) (*model.NUp, error) {
return pdfcpu.PDFBookletConfig(val, desc)
func PDFBookletConfig(val int, desc string, conf *model.Configuration) (*model.NUp, error) {
return pdfcpu.PDFBookletConfig(val, desc, conf)
}

// ImageBookletConfig returns an NUp configuration for Booklet-ing image files.
func ImageBookletConfig(val int, desc string) (*model.NUp, error) {
return pdfcpu.ImageBookletConfig(val, desc)
func ImageBookletConfig(val int, desc string, conf *model.Configuration) (*model.NUp, error) {
return pdfcpu.ImageBookletConfig(val, desc, conf)
}

// NUpFromImage creates a single page n-up PDF for one image
Expand Down
130 changes: 71 additions & 59 deletions pkg/api/test/booklet_test.go
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
)

func testBooklet(t *testing.T, msg string, inFiles []string, outFile string, selectedPages []string, desc string, n int, isImg bool) {
func testBooklet(t *testing.T, msg string, inFiles []string, outFile string, selectedPages []string, desc string, n int, isImg bool, conf *model.Configuration) {
t.Helper()

var (
Expand All @@ -33,19 +33,19 @@ func testBooklet(t *testing.T, msg string, inFiles []string, outFile string, sel
)

if isImg {
if booklet, err = api.ImageBookletConfig(n, desc); err != nil {
if booklet, err = api.ImageBookletConfig(n, desc, conf); err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}
} else {
if booklet, err = api.PDFBookletConfig(n, desc); err != nil {
if booklet, err = api.PDFBookletConfig(n, desc, conf); err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}
}

if err := api.BookletFile(inFiles, outFile, selectedPages, booklet, nil); err != nil {
if err := api.BookletFile(inFiles, outFile, selectedPages, booklet, conf); err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}
if err := api.ValidateFile(outFile, nil); err != nil {
if err := api.ValidateFile(outFile, conf); err != nil {
t.Fatalf("%s: %v\n", msg, err)
}
}
Expand All @@ -59,6 +59,7 @@ func TestBooklet(t *testing.T) {
outFile string
selectedPages []string
desc string
unit string
n int
isImg bool
}{
Expand All @@ -68,6 +69,7 @@ func TestBooklet(t *testing.T) {
filepath.Join(outDir, "BookletFromImagesA4_2Up.pdf"),
nil,
"p:A4, border:false, g:on, ma:25, bgcol:#beded9",
"points",
2,
true,
},
Expand All @@ -78,6 +80,7 @@ func TestBooklet(t *testing.T) {
filepath.Join(outDir, "BookletFromImagesA4_4Up.pdf"),
nil,
"p:A4, border:false, g:on, ma:25, bgcol:#beded9",
"points",
4,
true,
},
Expand All @@ -88,6 +91,7 @@ func TestBooklet(t *testing.T) {
filepath.Join(outDir, "BookletFromPDFA4_2Up.pdf"),
nil, // all pages
"p:A4, border:false, g:on, ma:10, bgcol:#beded9",
"points",
2,
false,
},
Expand All @@ -98,64 +102,72 @@ func TestBooklet(t *testing.T) {
filepath.Join(outDir, "BookletFromPDFA4_4Up.pdf"),
[]string{"1-"}, // all pages
"p:A4, border:off, guides:on, ma:10, bgcol:#beded9",
"points",
4,
false,
},

// // 4-up booklet from PDF on Ledger
// {"TestBookletFromPDF4UpLedger",
// []string{filepath.Join(inDir, "bookletTest.pdf")},
// filepath.Join(outDir, "BookletFromPDFLedger_4Up.pdf"),
// []string{"1-24"},
// "p:LedgerP, g:on, ma:10, bgcol:#f7e6c7",
// 4,
// false,
// },

// // 4-up booklet from PDF on Ledger where the number of pages don't fill the whole sheet
// {"TestBookletFromPDF4UpLedgerWithTrailingBlankPages",
// []string{filepath.Join(inDir, "bookletTest.pdf")},
// filepath.Join(outDir, "BookletFromPDFLedger_4UpWithTrailingBlankPages.pdf"),
// []string{"1-21"},
// "p:LedgerP, g:on, ma:10, bgcol:#f7e6c7",
// 4,
// false,
// },

// // 2-up booklet from PDF on Letter
// {"TestBookletFromPDF2UpLetter",
// []string{filepath.Join(inDir, "bookletTest.pdf")},
// filepath.Join(outDir, "BookletFromPDFLetter_2Up.pdf"),
// []string{"1-16"},
// "p:LetterP, g:on, ma:10, bgcol:#f7e6c7",
// 2,
// false,
// },

// // 2-up booklet from PDF on Letter where the number of pages don't fill the whole sheet
// {"TestBookletFromPDF2UpLetterWithTrailingBlankPages",
// []string{filepath.Join(inDir, "bookletTest.pdf")},
// filepath.Join(outDir, "BookletFromPDFLetter_2UpWithTrailingBlankPages.pdf"),
// []string{"1-14"},
// "p:LetterP, g:on, ma:10, bgcol:#f7e6c7",
// 2,
// false,
// },

// // 2-up multi folio booklet from PDF on A4 using 8 sheets per folio
// // using the default foliosize:8
// // Here we print 2 complete folios (2 x 8 sheets) + 1 partial folio
// // multi folio only makes sense for n = 2
// // See also https://www.instructables.com/How-to-bind-your-own-Hardback-Book/
// {"TestHardbackBookFromPDF",
// []string{filepath.Join(inDir, "WaldenFull.pdf")},
// filepath.Join(outDir, "HardbackBookFromPDF.pdf"),
// []string{"1-70"},
// "p:A4, multifolio:on, border:off, g:on, ma:10, bgcol:#beded9",
// 2,
// false,
// },
// 4-up booklet from PDF on Ledger
{"TestBookletFromPDF4UpLedger",
[]string{filepath.Join(inDir, "bookletTest.pdf")},
filepath.Join(outDir, "BookletFromPDFLedger_4Up.pdf"),
[]string{"1-24"},
"p:LedgerP, g:on, ma:10, bgcol:#f7e6c7",
"points",
4,
false,
},

// 4-up booklet from PDF on Ledger where the number of pages don't fill the whole sheet
{"TestBookletFromPDF4UpLedgerWithTrailingBlankPages",
[]string{filepath.Join(inDir, "bookletTest.pdf")},
filepath.Join(outDir, "BookletFromPDFLedger_4UpWithTrailingBlankPages.pdf"),
[]string{"1-21"},
"p:LedgerP, g:on, ma:10, bgcol:#f7e6c7",
"points",
4,
false,
},

// 2-up booklet from PDF on Letter
{"TestBookletFromPDF2UpLetter",
[]string{filepath.Join(inDir, "bookletTest.pdf")},
filepath.Join(outDir, "BookletFromPDFLetter_2Up.pdf"),
[]string{"1-16"},
"p:LetterP, g:on, ma:10, bgcol:#f7e6c7",
"points",
2,
false,
},

// 2-up booklet from PDF on Letter where the number of pages don't fill the whole sheet
{"TestBookletFromPDF2UpLetterWithTrailingBlankPages",
[]string{filepath.Join(inDir, "bookletTest.pdf")},
filepath.Join(outDir, "BookletFromPDFLetter_2UpWithTrailingBlankPages.pdf"),
[]string{"1-14"},
"p:LetterP, g:on, ma:10, bgcol:#f7e6c7",
"points",
2,
false,
},

// 2-up multi folio booklet from PDF on A4 using 8 sheets per folio
// using the default foliosize:8
// Here we print 2 complete folios (2 x 8 sheets) + 1 partial folio
// multi folio only makes sense for n = 2
// See also https://www.instructables.com/How-to-bind-your-own-Hardback-Book/
{"TestHardbackBookFromPDF",
[]string{filepath.Join(inDir, "WaldenFull.pdf")},
filepath.Join(outDir, "HardbackBookFromPDF.pdf"),
[]string{"1-70"},
"p:A4, multifolio:on, border:off, g:on, ma:10, bgcol:#beded9",
"points",
2,
false,
},
} {
testBooklet(t, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.n, tt.isImg)
conf := model.NewDefaultConfiguration()
conf.SetUnit(tt.unit)
testBooklet(t, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.n, tt.isImg, conf)
}
}
21 changes: 12 additions & 9 deletions pkg/api/test/grid_test.go
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
)

func testGrid(t *testing.T, msg string, inFiles []string, outFile string, selectedPages []string, desc string, rows, cols int, isImg bool) {
func testGrid(t *testing.T, msg string, inFiles []string, outFile string, selectedPages []string, desc string, rows, cols int, isImg bool, conf *model.Configuration) {
t.Helper()

var (
Expand All @@ -33,19 +33,19 @@ func testGrid(t *testing.T, msg string, inFiles []string, outFile string, select
)

if isImg {
if nup, err = api.ImageGridConfig(rows, cols, desc); err != nil {
if nup, err = api.ImageGridConfig(rows, cols, desc, conf); err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}
} else {
if nup, err = api.PDFGridConfig(rows, cols, desc); err != nil {
if nup, err = api.PDFGridConfig(rows, cols, desc, conf); err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}
}

if err := api.NUpFile(inFiles, outFile, selectedPages, nup, nil); err != nil {
if err := api.NUpFile(inFiles, outFile, selectedPages, nup, conf); err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}
if err := api.ValidateFile(outFile, nil); err != nil {
if err := api.ValidateFile(outFile, conf); err != nil {
t.Fatalf("%s: %v\n", msg, err)
}
}
Expand All @@ -60,24 +60,27 @@ func TestGrid(t *testing.T) {
outFile string
selectedPages []string
desc string
unit string
rows, cols int
isImg bool
}{
{"TestGridFromPDF",
[]string{filepath.Join(inDir, "read.go.pdf")},
filepath.Join(outDir, "GridFromPDF.pdf"),
nil, "form:LegalP, o:dr, border:off", 4, 6, false},
nil, "form:LegalP, o:dr, border:off", "points", 4, 6, false},

{"TestGridFromPDFWithCropBox",
[]string{filepath.Join(inDir, "grid_example.pdf")},
filepath.Join(outDir, "GridFromPDFWithCropBox.pdf"),
nil, "form:A5L, border:on, margin:0", 2, 1, false},
nil, "form:A5L, border:on, margin:0", "points", 2, 1, false},

{"TestGridFromImages",
imageFileNames(t, resDir),
filepath.Join(outDir, "GridFromImages.pdf"),
nil, "d:500 500, margin:20, bo:off", 1, 4, true},
nil, "d:500 500, margin:20, bo:off", "points", 1, 4, true},
} {
testGrid(t, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.rows, tt.cols, tt.isImg)
conf := model.NewDefaultConfiguration()
conf.SetUnit(tt.unit)
testGrid(t, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.rows, tt.cols, tt.isImg, conf)
}
}

0 comments on commit f7d021c

Please sign in to comment.