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

     1  /*
     2  Copyright 2023 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/cli"
    24  )
    25  
    26  /**************************************************************
    27   * All form related processing is optimized for Adobe Reader! *
    28   **************************************************************/
    29  
    30  func TestListFormFields(t *testing.T) {
    31  
    32  	msg := "TestListFormFields"
    33  	inFile := filepath.Join(samplesDir, "form", "demo", "english.pdf")
    34  
    35  	cmd := cli.ListFormFieldsCommand([]string{inFile}, conf)
    36  	if _, err := cli.Process(cmd); err != nil {
    37  		t.Fatalf("%s %s: %v\n", msg, inFile, err)
    38  	}
    39  }
    40  
    41  func TestRemoveFormFields(t *testing.T) {
    42  
    43  	msg := "TestRemoveFormFields"
    44  	inFile := filepath.Join(samplesDir, "form", "demo", "english.pdf")
    45  	outFile := filepath.Join(outDir, "removedField.pdf")
    46  
    47  	cmd := cli.RemoveFormFieldsCommand(inFile, outFile, []string{"dob1"}, conf)
    48  	if _, err := cli.Process(cmd); err != nil {
    49  		t.Fatalf("%s %s: %v\n", msg, inFile, err)
    50  	}
    51  }
    52  
    53  func TestResetFormFields(t *testing.T) {
    54  
    55  	for _, tt := range []struct {
    56  		msg     string
    57  		inFile  string
    58  		outFile string
    59  	}{
    60  		{"TestResetFormCorefont", "english.pdf", "english-reset.pdf"},        // Core font (Helvetica)
    61  		{"TestResetFormUserfont", "ukrainian.pdf", "ukrainian-reset.pdf"},    // User font (Roboto-Regular)
    62  		{"TestFormRTL", "arabic.pdf", "arabic-reset.pdf"},                    // User font RTL (Roboto-Regular)
    63  		{"TestResetFormCJK", "chineseSimple.pdf", "chineseSimple-reset.pdf"}, // User font CJK (UnifontMedium)
    64  		{"TestResetPersonForm", "person.pdf", "person-reset.pdf"},            // Person Form
    65  	} {
    66  		inFile := filepath.Join(samplesDir, "form", "demoSinglePage", tt.inFile)
    67  		outFile := filepath.Join(outDir, tt.outFile)
    68  
    69  		cmd := cli.ResetFormCommand(inFile, outFile, nil, conf)
    70  		if _, err := cli.Process(cmd); err != nil {
    71  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
    72  		}
    73  	}
    74  
    75  }
    76  
    77  func TestLockFormFields(t *testing.T) {
    78  
    79  	for _, tt := range []struct {
    80  		msg     string
    81  		inFile  string
    82  		outFile string
    83  	}{
    84  		{"TestLockFormEN", "english.pdf", "english-locked.pdf"},              // Core font (Helvetica)
    85  		{"TestLockFormUK", "ukrainian.pdf", "ukrainian-locked.pdf"},          // User font (Roboto-Regular)
    86  		{"TestLockFormRTL", "arabic.pdf", "arabic-locked.pdf"},               // User font RTL (Roboto-Regular)
    87  		{"TestLockFormCJK", "chineseSimple.pdf", "chineseSimple-locked.pdf"}, // User font CJK (UnifontMedium)
    88  		{"TestLockPersonForm", "person.pdf", "person-locked.pdf"},            // Person Form
    89  	} {
    90  		inFile := filepath.Join(samplesDir, "form", "demoSinglePage", tt.inFile)
    91  		outFile := filepath.Join(outDir, tt.outFile)
    92  
    93  		cmd := cli.LockFormCommand(inFile, outFile, nil, conf)
    94  		if _, err := cli.Process(cmd); err != nil {
    95  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
    96  		}
    97  	}
    98  }
    99  
   100  func TestUnlockFormFields(t *testing.T) {
   101  
   102  	for _, tt := range []struct {
   103  		msg     string
   104  		inFile  string
   105  		outFile string
   106  	}{
   107  		{"TestUnlockFormEN", "english-locked.pdf", "english-unlocked.pdf"},              // Core font (Helvetica)
   108  		{"TestUnlockFormUK", "ukrainian-locked.pdf", "ukrainian-unlocked.pdf"},          // User font (Roboto-Regular)
   109  		{"TestUnlockFormRTL", "arabic-locked.pdf", "arabic-unlocked.pdf"},               // User font RTL (Roboto-Regular)
   110  		{"TestUnlockFormCJK", "chineseSimple-locked.pdf", "chineseSimple-unlocked.pdf"}, // User font CJK (UnifontMedium)
   111  		{"TestUnlockPersonForm", "person-locked.pdf", "person-unlocked.pdf"},            // Person Form
   112  	} {
   113  		inFile := filepath.Join(samplesDir, "form", "lock", tt.inFile)
   114  		outFile := filepath.Join(outDir, tt.outFile)
   115  
   116  		cmd := cli.UnlockFormCommand(inFile, outFile, nil, conf)
   117  		if _, err := cli.Process(cmd); err != nil {
   118  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
   119  		}
   120  	}
   121  }
   122  
   123  func TestExportForm(t *testing.T) {
   124  
   125  	inDir := filepath.Join(samplesDir, "form", "demoSinglePage")
   126  
   127  	for _, tt := range []struct {
   128  		msg     string
   129  		inFile  string
   130  		outFile string
   131  	}{
   132  		{"TestExportFormEN", "english.pdf", "english.json"},              // Core font (Helvetica)
   133  		{"TestExportFormUK", "ukrainian.pdf", "ukrainian.json"},          // User font (Roboto-Regular)
   134  		{"TestExportFormRTL", "arabic.pdf", "arabic.json"},               // User font RTL (Roboto-Regular)
   135  		{"TestExportFormCJK", "chineseSimple.pdf", "chineseSimple.json"}, // User font CJK (UnifontMedium)
   136  		{"TestExportPersonForm", "person.pdf", "person.json"},            // Person Form
   137  	} {
   138  		inFile := filepath.Join(inDir, tt.inFile)
   139  		outFile := filepath.Join(outDir, tt.outFile)
   140  
   141  		cmd := cli.ExportFormCommand(inFile, outFile, conf)
   142  		if _, err := cli.Process(cmd); err != nil {
   143  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
   144  		}
   145  	}
   146  }
   147  
   148  func TestFillForm(t *testing.T) {
   149  
   150  	inDir := filepath.Join(samplesDir, "form", "demoSinglePage")
   151  	jsonDir := filepath.Join(samplesDir, "form", "fill")
   152  
   153  	for _, tt := range []struct {
   154  		msg        string
   155  		inFile     string
   156  		inFileJSON string
   157  		outFile    string
   158  	}{
   159  		{"TestFillFormEN", "english.pdf", "english.json", "english.pdf"},                    // Core font (Helvetica)
   160  		{"TestFillFormUK", "ukrainian.pdf", "ukrainian.json", "ukrainian.pdf"},              // User font (Roboto-Regular)
   161  		{"TestFillFormRTL", "arabic.pdf", "arabic.json", "arabic.pdf"},                      // User font RTL (Roboto-Regular)
   162  		{"TestFillFormCJK", "chineseSimple.pdf", "chineseSimple.json", "chineseSimple.pdf"}, // User font CJK (UnifontMedium)
   163  		{"TestFillPersonForm", "person.pdf", "person.json", "person.pdf"},                   // Person Form
   164  	} {
   165  		inFile := filepath.Join(inDir, tt.inFile)
   166  		inFileJSON := filepath.Join(jsonDir, tt.inFileJSON)
   167  		outFile := filepath.Join(outDir, tt.outFile)
   168  
   169  		cmd := cli.FillFormCommand(inFile, inFileJSON, outFile, conf)
   170  		if _, err := cli.Process(cmd); err != nil {
   171  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
   172  		}
   173  	}
   174  }
   175  
   176  func TestMultiFillFormJSON(t *testing.T) {
   177  
   178  	inDir := filepath.Join(samplesDir, "form", "demoSinglePage")
   179  	jsonDir := filepath.Join(samplesDir, "form", "multifill", "json")
   180  
   181  	for _, tt := range []struct {
   182  		msg        string
   183  		inFile     string
   184  		inFileJSON string
   185  	}{
   186  		{"TestMultiFillFormJSONEnglish", "english.pdf", "english.json"},
   187  		{"TestMultiFillFormJSONPerson", "person.pdf", "person.json"},
   188  	} {
   189  		inFile := filepath.Join(inDir, tt.inFile)
   190  		inFileJSON := filepath.Join(jsonDir, tt.inFileJSON)
   191  
   192  		cmd := cli.MultiFillFormCommand(inFile, inFileJSON, outDir, tt.inFile, false, conf)
   193  		if _, err := cli.Process(cmd); err != nil {
   194  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
   195  		}
   196  	}
   197  }
   198  
   199  func TestMultiFillFormJSONMerged(t *testing.T) {
   200  
   201  	inDir := filepath.Join(samplesDir, "form", "demoSinglePage")
   202  	jsonDir := filepath.Join(samplesDir, "form", "multifill", "json")
   203  
   204  	for _, tt := range []struct {
   205  		msg        string
   206  		inFile     string
   207  		inFileJSON string
   208  	}{
   209  		{"TestMultiFillFormJSONEnglish", "english.pdf", "english.json"},
   210  		{"TestMultiFillFormJSONPerson", "person.pdf", "person.json"},
   211  	} {
   212  		inFile := filepath.Join(inDir, tt.inFile)
   213  		inFileJSON := filepath.Join(jsonDir, tt.inFileJSON)
   214  
   215  		cmd := cli.MultiFillFormCommand(inFile, inFileJSON, outDir, tt.inFile, true, conf)
   216  		if _, err := cli.Process(cmd); err != nil {
   217  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
   218  		}
   219  	}
   220  }
   221  
   222  func TestMultiFillFormCSV(t *testing.T) {
   223  
   224  	inDir := filepath.Join(samplesDir, "form", "demoSinglePage")
   225  	csvDir := filepath.Join(samplesDir, "form", "multifill", "csv")
   226  
   227  	for _, tt := range []struct {
   228  		msg       string
   229  		inFile    string
   230  		inFileCSV string
   231  	}{
   232  		{"TestMultiFillFormCSVEnglish", "english.pdf", "english.csv"},
   233  		{"TestMultiFillFormCSVPerson", "person.pdf", "person.csv"},
   234  	} {
   235  
   236  		inFile := filepath.Join(inDir, tt.inFile)
   237  		inFileCSV := filepath.Join(csvDir, tt.inFileCSV)
   238  
   239  		cmd := cli.MultiFillFormCommand(inFile, inFileCSV, outDir, tt.inFile, false, conf)
   240  		if _, err := cli.Process(cmd); err != nil {
   241  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
   242  		}
   243  	}
   244  }
   245  
   246  func TestMultiFillFormCSVMerged(t *testing.T) {
   247  
   248  	inDir := filepath.Join(samplesDir, "form", "demoSinglePage")
   249  	csvDir := filepath.Join(samplesDir, "form", "multifill", "csv")
   250  
   251  	for _, tt := range []struct {
   252  		msg       string
   253  		inFile    string
   254  		inFileCSV string
   255  	}{
   256  		{"TestMultiFillFormCSVEnglish", "english.pdf", "english.csv"},
   257  		{"TestMultiFillFormCSVPerson", "person.pdf", "person.csv"},
   258  	} {
   259  
   260  		inFile := filepath.Join(inDir, tt.inFile)
   261  		inFileCSV := filepath.Join(csvDir, tt.inFileCSV)
   262  
   263  		cmd := cli.MultiFillFormCommand(inFile, inFileCSV, outDir, tt.inFile, false, conf)
   264  		if _, err := cli.Process(cmd); err != nil {
   265  			t.Fatalf("%s %s: %v\n", tt.msg, inFile, err)
   266  		}
   267  	}
   268  }