github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/resize_test.go (about) 1 /* 2 Copyright 2023 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" 25 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types" 26 ) 27 28 func TestResizeByScaleFactor(t *testing.T) { 29 msg := "TestResizeByScaleFactor" 30 31 inFile := filepath.Join(inDir, "test.pdf") 32 33 // Enlarge by scale factor 2. 34 res, err := pdfcpu.ParseResizeConfig("sc:2", types.POINTS) 35 if err != nil { 36 t.Fatalf("%s invalid resize configuration: %v\n", msg, err) 37 } 38 39 outFile := filepath.Join(samplesDir, "resize", "enlargeByScaleFactor.pdf") 40 if err := api.ResizeFile(inFile, outFile, nil, res, nil); err != nil { 41 t.Fatalf("%s resize: %v\n", msg, err) 42 } 43 44 // Shrink by 50%. 45 res, err = pdfcpu.ParseResizeConfig("sc:.5", types.POINTS) 46 if err != nil { 47 t.Fatalf("%s invalid resize configuration: %v\n", msg, err) 48 } 49 50 outFile = filepath.Join(samplesDir, "resize", "shrinkByScaleFactor.pdf") 51 if err := api.ResizeFile(inFile, outFile, nil, res, nil); err != nil { 52 t.Fatalf("%s resize: %v\n", msg, err) 53 } 54 } 55 56 func TestResizeByWidthOrHeight(t *testing.T) { 57 msg := "TestResizeByWidthOrHeight" 58 59 inFile := filepath.Join(inDir, "test.pdf") 60 61 // Set width to 200 points. 62 res, err := pdfcpu.ParseResizeConfig("dim:200 0", types.POINTS) 63 if err != nil { 64 t.Fatalf("%s invalid resize configuration: %v\n", msg, err) 65 } 66 67 outFile := filepath.Join(samplesDir, "resize", "resizeByWidth.pdf") 68 if err := api.ResizeFile(inFile, outFile, nil, res, nil); err != nil { 69 t.Fatalf("%s resize: %v\n", msg, err) 70 } 71 72 // Set height to 200 mm. 73 res, err = pdfcpu.ParseResizeConfig("dim:0 200", types.MILLIMETRES) 74 if err != nil { 75 t.Fatalf("%s invalid resize configuration: %v\n", msg, err) 76 } 77 78 outFile = filepath.Join(samplesDir, "resize", "resizeByHeight.pdf") 79 if err := api.ResizeFile(inFile, outFile, nil, res, nil); err != nil { 80 t.Fatalf("%s resize: %v\n", msg, err) 81 } 82 } 83 84 func TestResizeToFormSize(t *testing.T) { 85 msg := "TestResizeToPaperSize" 86 87 inFile := filepath.Join(inDir, "test.pdf") 88 89 // Resize to A3 and keep orientation. 90 res, err := pdfcpu.ParseResizeConfig("form:A3", types.POINTS) 91 if err != nil { 92 t.Fatalf("%s invalid resize configuration: %v\n", msg, err) 93 } 94 95 outFile := filepath.Join(samplesDir, "resize", "resizeToA3.pdf") 96 if err := api.ResizeFile(inFile, outFile, nil, res, nil); err != nil { 97 t.Fatalf("%s resize: %v\n", msg, err) 98 } 99 100 // Resize to A4 and enforce orientation (here landscape mode). 101 res, err = pdfcpu.ParseResizeConfig("form:A4L", types.POINTS) 102 if err != nil { 103 t.Fatalf("%s invalid resize configuration: %v\n", msg, err) 104 } 105 106 outFile = filepath.Join(samplesDir, "resize", "resizeToA4L.pdf") 107 if err := api.ResizeFile(inFile, outFile, nil, res, nil); err != nil { 108 t.Fatalf("%s resize: %v\n", msg, err) 109 } 110 } 111 112 func TestResizeToDimensions(t *testing.T) { 113 msg := "TestResizeToDimensions" 114 115 inFile := filepath.Join(inDir, "test.pdf") 116 117 // Resize to 400 x 200 and keep orientation of input file. 118 // Apply background color to unused space. 119 res, err := pdfcpu.ParseResizeConfig("dim:400 200, bgcol:#E9967A", types.POINTS) 120 if err != nil { 121 t.Fatalf("%s invalid resize configuration: %v\n", msg, err) 122 } 123 124 outFile := filepath.Join(samplesDir, "resize", "resizeToDimensionsKeep.pdf") 125 if err := api.ResizeFile(inFile, outFile, nil, res, nil); err != nil { 126 t.Fatalf("%s resize: %v\n", msg, err) 127 } 128 129 // Resize to 400 x 200 and enforce new orientation. 130 // Render border of original crop box. 131 res, err = pdfcpu.ParseResizeConfig("dim:400 200, enforce:true, border:on", types.POINTS) 132 if err != nil { 133 t.Fatalf("%s invalid resize configuration: %v\n", msg, err) 134 } 135 136 outFile = filepath.Join(samplesDir, "resize", "resizeToDimensionsEnforce.pdf") 137 if err := api.ResizeFile(inFile, outFile, nil, res, nil); err != nil { 138 t.Fatalf("%s resize: %v\n", msg, err) 139 } 140 }