github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/nup_test.go (about)

     1  /*
     2  Copyright 2020 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 testNUp(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  		nup *model.NUp
    33  		err error
    34  	)
    35  
    36  	if isImg {
    37  		if nup, err = api.ImageNUpConfig(n, desc, conf); err != nil {
    38  			t.Fatalf("%s %s: %v\n", msg, outFile, err)
    39  		}
    40  	} else {
    41  		if nup, err = api.PDFNUpConfig(n, desc, conf); err != nil {
    42  			t.Fatalf("%s %s: %v\n", msg, outFile, err)
    43  		}
    44  	}
    45  
    46  	cmd := cli.NUpCommand(inFiles, outFile, selectedPages, nup, 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 TestNUpCommand(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  		{"TestNUpFromPDF",
    68  			[]string{filepath.Join(inDir, "Acroforms2.pdf")},
    69  			filepath.Join(outDir, "Acroforms2.pdf"),
    70  			nil,
    71  			"",
    72  			"points",
    73  			4,
    74  			false},
    75  
    76  		{"TestNUpFromSingleImage",
    77  			[]string{filepath.Join(resDir, "pdfchip3.png")},
    78  			filepath.Join(outDir, "out.pdf"),
    79  			nil,
    80  			"form:A3L",
    81  			"points",
    82  			9,
    83  			true},
    84  
    85  		{"TestNUpFromImages",
    86  			[]string{
    87  				filepath.Join(resDir, "pdfchip3.png"),
    88  				filepath.Join(resDir, "demo.png"),
    89  				filepath.Join(resDir, "snow.jpg"),
    90  			},
    91  			filepath.Join(outDir, "out1.pdf"),
    92  			nil,
    93  			"form:Tabloid, bo:off, ma:0, enforce:off",
    94  			"points",
    95  			6,
    96  			true},
    97  	} {
    98  		conf := model.NewDefaultConfiguration()
    99  		conf.SetUnit(tt.unit)
   100  		testNUp(t, tt.msg, tt.inFiles, tt.outFile, tt.selectedPages, tt.desc, tt.n, tt.isImg, conf)
   101  	}
   102  }