github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/importImages_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"
    26  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
    27  )
    28  
    29  func testImportImages(t *testing.T, msg string, imgFiles []string, outFile, impConf string) {
    30  	t.Helper()
    31  	var err error
    32  
    33  	outFile = filepath.Join(outDir, outFile)
    34  
    35  	// The default import conf uses the special pos:full argument
    36  	// which overrides all other import conf parms.
    37  	imp := pdfcpu.DefaultImportConfig()
    38  	if impConf != "" {
    39  		if imp, err = api.Import(impConf, types.POINTS); err != nil {
    40  			t.Fatalf("%s %s: %v\n", msg, outFile, err)
    41  		}
    42  	}
    43  	cmd := cli.ImportImagesCommand(imgFiles, outFile, imp, conf)
    44  	if _, err := cli.Process(cmd); err != nil {
    45  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    46  	}
    47  	if err := validateFile(t, outFile, conf); err != nil {
    48  		t.Fatalf("%s: %v\n", msg, err)
    49  	}
    50  }
    51  
    52  func TestImportCommand(t *testing.T) {
    53  	for _, tt := range []struct {
    54  		msg      string
    55  		imgFiles []string
    56  		outFile  string
    57  		impConf  string
    58  	}{
    59  		// Render image on an A4 portrait mode page.
    60  		{"TestCenteredGraySepia",
    61  			[]string{filepath.Join(resDir, "mountain.jpg")},
    62  			"CenteredGraySepia.pdf",
    63  			"f:A4, pos:c, bgcol:#beded9"},
    64  
    65  		// Import another image as a new page of testfile1 and convert image to gray.
    66  		{"TestCenteredGraySepia",
    67  			[]string{filepath.Join(resDir, "mountain.png")},
    68  			"CenteredGraySepia.pdf",
    69  			"f:A4, pos:c, sc:.75, bgcol:#beded9, gray:true"},
    70  
    71  		// Import another image as a new page of testfile1 and apply a sepia filter.
    72  		{"TestCenteredGraySepia",
    73  			[]string{filepath.Join(resDir, "mountain.webp")},
    74  			"CenteredGraySepia.pdf",
    75  			"f:A4, pos:c, sc:.9, bgcol:#beded9, sepia:true"},
    76  
    77  		// Import another image as a new page of testfile1.
    78  		{"TestCenteredGraySepia",
    79  			[]string{filepath.Join(resDir, "mountain.tif")},
    80  			"CenteredGraySepia.pdf",
    81  			"f:A4, pos:c, sc:1, bgcol:#beded9"},
    82  
    83  		// Page dimensions match image dimensions.
    84  		{"TestFull",
    85  			imageFileNames(t, filepath.Join(resDir)),
    86  			"Full.pdf",
    87  			"pos:full"},
    88  	} {
    89  		testImportImages(t, tt.msg, tt.imgFiles, tt.outFile, tt.impConf)
    90  	}
    91  }