github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/importImages_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 "bufio" 21 "bytes" 22 "io" 23 "os" 24 "path/filepath" 25 "testing" 26 27 "github.com/pdfcpu/pdfcpu/pkg/api" 28 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu" 29 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types" 30 ) 31 32 func testImportImages(t *testing.T, msg string, imgFiles []string, outFile, impConf string) { 33 t.Helper() 34 var err error 35 36 // The default import conf uses the special pos:full argument 37 // which overrides all other import conf parms. 38 imp := pdfcpu.DefaultImportConfig() 39 if impConf != "" { 40 if imp, err = api.Import(impConf, types.POINTS); err != nil { 41 t.Fatalf("%s %s: %v\n", msg, outFile, err) 42 } 43 } 44 if err := api.ImportImagesFile(imgFiles, outFile, imp, nil); err != nil { 45 t.Fatalf("%s %s: %v\n", msg, outFile, err) 46 } 47 if err := api.ValidateFile(outFile, nil); err != nil { 48 t.Fatalf("%s: %v\n", msg, err) 49 } 50 } 51 52 func TestImportImages(t *testing.T) { 53 54 outDir := filepath.Join(samplesDir, "import") 55 56 testFile1 := filepath.Join(outDir, "CenteredGraySepia.pdf") 57 os.Remove(testFile1) 58 59 testFile2 := filepath.Join(outDir, "Full.pdf") 60 os.Remove(testFile2) 61 62 for _, tt := range []struct { 63 msg string 64 imgFiles []string 65 outFile string 66 impConf string 67 }{ 68 // Render image on an A4 portrait mode page. 69 {"TestCenteredGraySepia", 70 []string{filepath.Join(resDir, "mountain.jpg")}, 71 testFile1, 72 "f:A4, pos:c, bgcol:#beded9"}, 73 74 // Import another image as a new page of testfile1 and convert image to gray. 75 {"TestCenteredGraySepia", 76 []string{filepath.Join(resDir, "mountain.png")}, 77 testFile1, 78 "f:A4, pos:c, sc:.75, bgcol:#beded9, gray:true"}, 79 80 // Import another image as a new page of testfile1 and apply a sepia filter. 81 {"TestCenteredGraySepia", 82 []string{filepath.Join(resDir, "mountain.webp")}, 83 testFile1, 84 "f:A4, pos:c, sc:.9, bgcol:#beded9, sepia:true"}, 85 86 // Import another image as a new page of testfile1. 87 {"TestCenteredGraySepia", 88 []string{filepath.Join(resDir, "mountain.tif")}, 89 testFile1, 90 "f:A4, pos:c, sc:1, bgcol:#beded9"}, 91 92 // Page dimensions match image dimensions. 93 {"TestFull", 94 imageFileNames(t, resDir), 95 testFile2, 96 "pos:full"}, 97 } { 98 testImportImages(t, tt.msg, tt.imgFiles, tt.outFile, tt.impConf) 99 } 100 } 101 102 func TestMemBasedWriterPanic(t *testing.T) { 103 104 imgFiles := []string{filepath.Join(resDir, "logoSmall.png")} 105 106 rr := make([]io.Reader, len(imgFiles)) 107 for i, fn := range imgFiles { 108 f, err := os.Open(fn) 109 if err != nil { 110 t.Fatal(err) 111 } 112 rr[i] = bufio.NewReader(f) 113 } 114 115 outBuf := &bytes.Buffer{} 116 117 if err := api.ImportImages(nil, outBuf, rr, nil, nil); err != nil { 118 t.Fatal(err) 119 } 120 121 }