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