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

     1  /*
     2  Copyright 2024 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 TestZoomInByFactor(t *testing.T) {
    29  	msg := "TestZoomInByFactor"
    30  
    31  	inFile := filepath.Join(inDir, "test.pdf")
    32  
    33  	zoom, err := pdfcpu.ParseZoomConfig("factor:2", types.POINTS)
    34  	if err != nil {
    35  		t.Fatalf("%s invalid zoom configuration: %v\n", msg, err)
    36  	}
    37  	outFile := filepath.Join(outDir, "zoomInByFactor2.pdf")
    38  	cmd := cli.ZoomCommand(inFile, outFile, nil, zoom, conf)
    39  	if _, err := cli.Process(cmd); err != nil {
    40  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    41  	}
    42  
    43  	zoom, err = pdfcpu.ParseZoomConfig("factor:4", types.POINTS)
    44  	if err != nil {
    45  		t.Fatalf("%s invalid zoom configuration: %v\n", msg, err)
    46  	}
    47  	outFile = filepath.Join(outDir, "zoomInByFactor4.pdf")
    48  	cmd = cli.ZoomCommand(inFile, outFile, nil, zoom, conf)
    49  	if _, err := cli.Process(cmd); err != nil {
    50  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    51  	}
    52  }
    53  
    54  func TestZoomOutByFactor(t *testing.T) {
    55  	msg := "TestZoomOutByFactor"
    56  
    57  	inFile := filepath.Join(inDir, "test.pdf")
    58  
    59  	zoom, err := pdfcpu.ParseZoomConfig("factor:.5", types.POINTS)
    60  	if err != nil {
    61  		t.Fatalf("%s invalid zoom configuration: %v\n", msg, err)
    62  	}
    63  	outFile := filepath.Join(outDir, "zoomOutByFactor05.pdf")
    64  	cmd := cli.ZoomCommand(inFile, outFile, nil, zoom, conf)
    65  	if _, err := cli.Process(cmd); err != nil {
    66  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    67  	}
    68  
    69  	zoom, err = pdfcpu.ParseZoomConfig("factor:.25, border:true", types.POINTS)
    70  	if err != nil {
    71  		t.Fatalf("%s invalid zoom configuration: %v\n", msg, err)
    72  	}
    73  	outFile = filepath.Join(outDir, "zoomOutByFactor025.pdf")
    74  	cmd = cli.ZoomCommand(inFile, outFile, nil, zoom, conf)
    75  	if _, err := cli.Process(cmd); err != nil {
    76  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    77  	}
    78  }
    79  
    80  func TestZoomOutByHorizontalMargin(t *testing.T) {
    81  	// Zoom out of page content resulting in a preferred horizontal margin.
    82  	msg := "TestZoomOutByHMargin"
    83  	inFile := filepath.Join(inDir, "test.pdf")
    84  
    85  	zoom, err := pdfcpu.ParseZoomConfig("hmargin:149", types.POINTS)
    86  	if err != nil {
    87  		t.Fatalf("%s invalid zoom configuration: %v\n", msg, err)
    88  	}
    89  	outFile := filepath.Join(outDir, "zoomOutByHMarginPoints.pdf")
    90  	cmd := cli.ZoomCommand(inFile, outFile, nil, zoom, conf)
    91  	if _, err := cli.Process(cmd); err != nil {
    92  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    93  	}
    94  
    95  	zoom, err = pdfcpu.ParseZoomConfig("hmargin:1, border:true, bgcol:lightgray", types.CENTIMETRES)
    96  	if err != nil {
    97  		t.Fatalf("%s invalid zoom configuration: %v\n", msg, err)
    98  	}
    99  	outFile = filepath.Join(outDir, "zoomOutByHMarginCm.pdf")
   100  	cmd = cli.ZoomCommand(inFile, outFile, nil, zoom, conf)
   101  	if _, err := cli.Process(cmd); err != nil {
   102  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   103  	}
   104  }
   105  
   106  func TestZoomOutByVerticalMargin(t *testing.T) {
   107  	// Zoom out of page content resulting in a preferred vertical margin.
   108  	msg := "TestZoomOutByVMargin"
   109  	inFile := filepath.Join(inDir, "test.pdf")
   110  
   111  	zoom, err := pdfcpu.ParseZoomConfig("vmargin:1", types.INCHES)
   112  	if err != nil {
   113  		t.Fatalf("%s invalid zoom configuration: %v\n", msg, err)
   114  	}
   115  	outFile := filepath.Join(outDir, "zoomOutByVMarginInches.pdf")
   116  	cmd := cli.ZoomCommand(inFile, outFile, nil, zoom, conf)
   117  	if _, err := cli.Process(cmd); err != nil {
   118  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   119  	}
   120  
   121  	zoom, err = pdfcpu.ParseZoomConfig("vmargin:30, border:false, bgcol:lightgray", types.MILLIMETRES)
   122  	if err != nil {
   123  		t.Fatalf("%s invalid zoom configuration: %v\n", msg, err)
   124  	}
   125  	outFile = filepath.Join(outDir, "zoomOutByVMarginMm.pdf")
   126  	cmd = cli.ZoomCommand(inFile, outFile, nil, zoom, conf)
   127  	if _, err := cli.Process(cmd); err != nil {
   128  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   129  	}
   130  }