github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/grid_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 testGrid(t *testing.T, msg string, inFiles []string, outFile string, selectedPages []string, desc string, rows, cols 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.ImageGridConfig(rows, cols, desc, conf); err != nil {
    37  			t.Fatalf("%s %s: %v\n", msg, outFile, err)
    38  		}
    39  	} else {
    40  		if nup, err = api.PDFGridConfig(rows, cols, 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 TestGrid(t *testing.T) {
    54  
    55  	outDir := filepath.Join(samplesDir, "grid")
    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  		rows, cols    int
    65  		isImg         bool
    66  	}{
    67  		{"TestGridFromPDF",
    68  			[]string{filepath.Join(inDir, "read.go.pdf")},
    69  			filepath.Join(outDir, "GridFromPDF.pdf"),
    70  			nil, "form:LegalP, o:dr, border:off", "points", 4, 6, false},
    71  
    72  		{"TestGridFromPDFWithCropBox",
    73  			[]string{filepath.Join(inDir, "grid_example.pdf")},
    74  			filepath.Join(outDir, "GridFromPDFWithCropBox.pdf"),
    75  			nil, "form:A5L, border:on, margin:0", "points", 2, 1, false},
    76  
    77  		{"TestGridFromImages",
    78  			imageFileNames(t, resDir),
    79  			filepath.Join(outDir, "GridFromImages.pdf"),
    80  			nil, "d:500 500, margin:20, bo:off", "points", 1, 4, true},
    81  	} {
    82  		conf := model.NewDefaultConfiguration()
    83  		conf.SetUnit(tt.unit)
    84  		testGrid(t, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.rows, tt.cols, tt.isImg, conf)
    85  	}
    86  }