github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/extract_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/cli"
    24  )
    25  
    26  func TestExtractImagesCommand(t *testing.T) {
    27  	msg := "TestExtractImagesCommand"
    28  
    29  	// Extract all images for each PDF file into outDir.
    30  	cmd := cli.ExtractImagesCommand("", outDir, nil, conf)
    31  	//for _, f := range allPDFs(t, inDir) {
    32  	for _, f := range []string{"5116.DCT_Filter.pdf", "testImage.pdf", "go.pdf"} {
    33  		inFile := filepath.Join(inDir, f)
    34  		cmd.InFile = &inFile
    35  		// Extract all images.
    36  		if _, err := cli.Process(cmd); err != nil {
    37  			t.Fatalf("%s %s: %v\n", msg, inFile, err)
    38  		}
    39  	}
    40  
    41  	// Extract all images for inFile starting with page 1 into outDir.
    42  	inFile := filepath.Join(inDir, "testImage.pdf")
    43  	cmd = cli.ExtractImagesCommand(inFile, outDir, []string{"1-"}, conf)
    44  	if _, err := cli.Process(cmd); err != nil {
    45  		t.Fatalf("%s %s: %v\n", msg, inFile, err)
    46  	}
    47  }
    48  
    49  func TestExtractFontsCommand(t *testing.T) {
    50  	msg := "TestExtractFontsCommand"
    51  
    52  	// Extract fonts for all pages for the following 3 PDF files into outDir.
    53  	cmd := cli.ExtractFontsCommand("", outDir, nil, conf)
    54  	for _, fn := range []string{"5116.DCT_Filter.pdf", "testImage.pdf", "go.pdf"} {
    55  		fn = filepath.Join(inDir, fn)
    56  		cmd.InFile = &fn
    57  		if _, err := cli.Process(cmd); err != nil {
    58  			t.Fatalf("%s %s: %v\n", msg, fn, err)
    59  		}
    60  	}
    61  
    62  	// Extract fonts for pages 1-3 into outDir.
    63  	inFile := filepath.Join(inDir, "go.pdf")
    64  	cmd = cli.ExtractFontsCommand(inFile, outDir, []string{"1-3"}, conf)
    65  	if _, err := cli.Process(cmd); err != nil {
    66  		t.Fatalf("%s %s: %v\n", msg, inFile, err)
    67  	}
    68  }
    69  
    70  func TestExtractPagesCommand(t *testing.T) {
    71  	msg := "TestExtractPagesCommand"
    72  	// Extract page #1 into outDir.
    73  	inFile := filepath.Join(inDir, "TheGoProgrammingLanguageCh1.pdf")
    74  	cmd := cli.ExtractPagesCommand(inFile, outDir, []string{"1"}, conf)
    75  	if _, err := cli.Process(cmd); err != nil {
    76  		t.Fatalf("%s %s: %v\n", msg, inFile, err)
    77  	}
    78  }
    79  
    80  func TestExtractContentCommand(t *testing.T) {
    81  	msg := "TestExtractContentCommand"
    82  	// Extract content of all pages into outDir.
    83  	inFile := filepath.Join(inDir, "5116.DCT_Filter.pdf")
    84  	cmd := cli.ExtractContentCommand(inFile, outDir, nil, conf)
    85  	if _, err := cli.Process(cmd); err != nil {
    86  		t.Fatalf("%s %s: %v\n", msg, inFile, err)
    87  	}
    88  }
    89  
    90  func TestExtractMetadataCommand(t *testing.T) {
    91  	msg := "TestExtractMetadataCommand"
    92  	// Extract metadata into outDir.
    93  	inFile := filepath.Join(inDir, "TheGoProgrammingLanguageCh1.pdf")
    94  	cmd := cli.ExtractMetadataCommand(inFile, outDir, conf)
    95  	if _, err := cli.Process(cmd); err != nil {
    96  		t.Fatalf("%s %s: %v\n", msg, inFile, err)
    97  	}
    98  }