github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/split_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 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" 25 ) 26 27 // Split a test PDF file up into single page PDFs (using a split span of 1). 28 func TestSplitCommand(t *testing.T) { 29 msg := "TestSplitCommand" 30 fileName := "Acroforms2.pdf" 31 inFile := filepath.Join(inDir, fileName) 32 span := 1 33 34 conf := model.NewDefaultConfiguration() 35 36 cmd := cli.SplitCommand(inFile, outDir, span, conf) 37 if _, err := cli.Process(cmd); err != nil { 38 t.Fatalf("%s span=%d %s: %v\n", msg, span, inFile, err) 39 } 40 } 41 42 // Split a test PDF file up into PDFs with 2 pages each (using a split span of 2). 43 func TestSplitBySpanCommand(t *testing.T) { 44 msg := "TestSplitBySpanCommand" 45 fileName := "CenterOfWhy.pdf" 46 inFile := filepath.Join(inDir, fileName) 47 span := 2 48 49 cmd := cli.SplitCommand(inFile, outDir, span, conf) 50 if _, err := cli.Process(cmd); err != nil { 51 t.Fatalf("%s span=%d %s: %v\n", msg, span, inFile, err) 52 } 53 } 54 55 // Split a PDF along its defined bookmarks on level 1 or 2 56 func TestSplitByBookmarkCommand(t *testing.T) { 57 msg := "TestSplitByBookmarkCommand" 58 fileName := "5116.DCT_Filter.pdf" 59 inFile := filepath.Join(inDir, fileName) 60 61 span := 0 // This means we are going to split by bookmarks. 62 63 cmd := cli.SplitCommand(inFile, outDir, span, conf) 64 if _, err := cli.Process(cmd); err != nil { 65 t.Fatalf("%s %s: %v\n", msg, inFile, err) 66 } 67 } 68 69 func TestSplitByPageNrCommand(t *testing.T) { 70 msg := "TestSplitByPageNrCommand" 71 fileName := "5116.DCT_Filter.pdf" 72 inFile := filepath.Join(inDir, fileName) 73 74 // Generate page section 1 75 // Generate page section 2-9 76 // Generate page section 10-49 77 // Generate page section 50-last page 78 79 cmd := cli.SplitByPageNrCommand(inFile, outDir, []int{2, 10, 50}, conf) 80 if _, err := cli.Process(cmd); err != nil { 81 t.Fatalf("%s %s: %v\n", msg, inFile, err) 82 } 83 }