github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/collect_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  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/pdfcpu/pdfcpu/pkg/api"
    24  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
    25  )
    26  
    27  func TestCollect(t *testing.T) {
    28  	msg := "TestCollect"
    29  
    30  	inFile := filepath.Join(inDir, "pike-stanford.pdf")
    31  	outFile := filepath.Join(outDir, "myPageSequence.pdf")
    32  
    33  	// Start with all odd pages but page 1, then append pages 8-11 and the last page.
    34  	if err := api.CollectFile(inFile, outFile, []string{"odd", "!1", "8-11", "l"}, nil); err != nil {
    35  		t.Fatalf("%s: %v\n", msg, err)
    36  	}
    37  
    38  	if err := api.ValidateFile(outFile, nil); err != nil {
    39  		t.Fatalf("%s: %v\n", msg, err)
    40  	}
    41  }
    42  
    43  func TestCollectLowLevel(t *testing.T) {
    44  	msg := "TestCollectLowLevel"
    45  	inFile := filepath.Join(inDir, "pike-stanford.pdf")
    46  	outFile := filepath.Join(outDir, "MyCollectedPages.pdf")
    47  
    48  	// Create a context.
    49  	ctx, err := api.ReadContextFile(inFile)
    50  	if err != nil {
    51  		t.Fatalf("%s readContext: %v\n", msg, err)
    52  	}
    53  
    54  	// Collect pages.
    55  	selectedPages, err := api.PagesForPageCollection(ctx.PageCount, []string{"odd", "!1", "8-11", "l"})
    56  	if err != nil {
    57  		t.Fatalf("%s PagesForPageCollection: %v\n", msg, err)
    58  	}
    59  
    60  	usePgCache := true
    61  	ctxNew, err := pdfcpu.ExtractPages(ctx, selectedPages, usePgCache)
    62  	if err != nil {
    63  		t.Fatalf("%s ExtractPages: %v\n", msg, err)
    64  	}
    65  
    66  	// Here you can process this single page PDF context.
    67  
    68  	// Write context to file.
    69  	if err := api.WriteContextFile(ctxNew, outFile); err != nil {
    70  		t.Fatalf("%s write: %v\n", msg, err)
    71  	}
    72  }