|
| 1 | +/* |
| 2 | + * This file is subject to the terms and conditions defined in |
| 3 | + * file 'LICENSE.md', which is part of this source code package. |
| 4 | + */ |
| 5 | + |
| 6 | +package e2etest |
| 7 | + |
| 8 | +import ( |
| 9 | + "io/ioutil" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "runtime/debug" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + |
| 18 | + "github.com/unidoc/unipdf/v3/annotator" |
| 19 | + "github.com/unidoc/unipdf/v3/fdf" |
| 20 | + "github.com/unidoc/unipdf/v3/model" |
| 21 | +) |
| 22 | + |
| 23 | +// FDF merge tests merge FDF data into template PDF data and flattens to an output PDF file. |
| 24 | +// Output files are checked with ghostscript and memory consumption is measured. |
| 25 | +// Set environment variables: |
| 26 | +// UNIDOC_E2E_FORCE_TESTS to "1" to force the tests to execute. |
| 27 | +// UNIDOC_FDFMERGE_TESTDATA to the path of the corpus folder. |
| 28 | +// UNIDOC_GS_BIN_PATH to the path of the ghostscript binary (gs) for validation. |
| 29 | +var ( |
| 30 | + fdfMergeCorpusFolder = os.Getenv("UNIDOC_FDFMERGE_TESTDATA") |
| 31 | +) |
| 32 | + |
| 33 | +// fdfMergeHashes defines a list of known output hashes to ensure that the output is constant. |
| 34 | +// If there is a change in hash need to find out why and update only if the change is accepted. |
| 35 | +var fdfMergeHashes = map[string]string{ |
| 36 | + "NW_null_Business_V04.fdf": "6e33f219994e4b9ee1e1843c976504df", |
| 37 | + "NW_null_Business_V05.fdf": "ff1f8bd39f9be9844a6d85bafe07c790", |
| 38 | + "NW_null_Business_V05.v1.2.fdf": "ff1f8bd39f9be9844a6d85bafe07c790", |
| 39 | + "NW_null_Contract_V04.fdf": "a54f4b42dc34997cfb701ef647cdbdfe", |
| 40 | + "N_null_Contract.fdf": "c173340d6492984532cf51a4f5ceb4b6", |
| 41 | + "Network_Contract_V01.fdf": "0ae2537bf8a8366aa97c1ca965b88d1f", |
| 42 | + "checkmark_check.fdf": "8892cdb01318421f8d198233b80ab8e3", |
| 43 | + "checkmark_circle.fdf": "3b1e6ef6aae2a7497b090e0960d2c163", |
| 44 | + "checkmark_cross.fdf": "6b16b6d7437a3f59a7e9e72c1ecfd59b", |
| 45 | + "checkmark_diamond.fdf": "123488e428914832f21e213339ed74f1", |
| 46 | + "checkmark_square.fdf": "d0ac69dac7a933e440a5005b1712edeb", |
| 47 | + "checkmark_star.fdf": "1326f152fb8158dffc08e5bb51cba1bc", |
| 48 | + "test_fail.fdf": "9a90cef679d6b4c13017c73c2528ca75", |
| 49 | +} |
| 50 | + |
| 51 | +// Test filling (fdf merge) and flattening form data and annotations. |
| 52 | +func TestFdfMerging(t *testing.T) { |
| 53 | + if len(fdfMergeCorpusFolder) == 0 { |
| 54 | + if forceTest { |
| 55 | + t.Fatalf("UNIDOC_FDFMERGE_TESTDATA not set") |
| 56 | + } |
| 57 | + t.Skipf("UNIDOC_FDFMERGE_TESTDATA not set") |
| 58 | + } |
| 59 | + |
| 60 | + files, err := ioutil.ReadDir(fdfMergeCorpusFolder) |
| 61 | + if err != nil { |
| 62 | + if forceTest { |
| 63 | + t.Fatalf("Error opening %s: %v", fdfMergeCorpusFolder, err) |
| 64 | + } |
| 65 | + t.Skipf("Skipping flatten bench - unable to open UNIDOC_FDFMERGE_TESTDATA (%s)", fdfMergeCorpusFolder) |
| 66 | + } |
| 67 | + |
| 68 | + // Make a temporary folder and clean up after. |
| 69 | + tempdir, err := ioutil.TempDir("", "unidoc_fdfmerge") |
| 70 | + require.NoError(t, err) |
| 71 | + defer os.RemoveAll(tempdir) |
| 72 | + |
| 73 | + matchcount := 0 |
| 74 | + for _, file := range files { |
| 75 | + if strings.ToLower(filepath.Ext(file.Name())) != ".fdf" { |
| 76 | + continue |
| 77 | + } |
| 78 | + fdfPath := filepath.Join(fdfMergeCorpusFolder, file.Name()) |
| 79 | + bareName := strings.TrimSuffix(file.Name(), ".fdf") |
| 80 | + pdfPath := filepath.Join(fdfMergeCorpusFolder, bareName+".pdf") |
| 81 | + |
| 82 | + // Ensure memory is garbage collected prior to running for consistency. |
| 83 | + debug.FreeOSMemory() |
| 84 | + |
| 85 | + t.Logf("%s", file.Name()) |
| 86 | + params := fdfMergeParams{ |
| 87 | + templatePath: pdfPath, |
| 88 | + fdfPath: fdfPath, |
| 89 | + outPath: filepath.Join(tempdir, "filled_flatten_1_"+bareName+".pdf"), |
| 90 | + gsValidation: len(ghostscriptBinPath) > 0, |
| 91 | + } |
| 92 | + fdfMergeSingle(t, params) |
| 93 | + |
| 94 | + hash, err := hashFile(params.outPath) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + knownHash, has := fdfMergeHashes[file.Name()] |
| 98 | + if has { |
| 99 | + require.Equal(t, knownHash, hash) |
| 100 | + matchcount++ |
| 101 | + } else { |
| 102 | + t.Logf("Output: %s", params.outPath) |
| 103 | + t.Logf("%s - hash: %s not in the list of known hashes", file.Name(), hash) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Ensure all the defined hashes were found. |
| 108 | + require.Equal(t, len(fdfMergeHashes), matchcount) |
| 109 | + |
| 110 | + t.Logf("FDF merge benchmark complete for %d cases in %s", matchcount, fdfMergeCorpusFolder) |
| 111 | +} |
| 112 | + |
| 113 | +type fdfMergeParams struct { |
| 114 | + templatePath string // template PDF file. |
| 115 | + fdfPath string // form data FDF file. |
| 116 | + outPath string |
| 117 | + gsValidation bool |
| 118 | +} |
| 119 | + |
| 120 | +func fdfMergeSingle(t *testing.T, params fdfMergeParams) { |
| 121 | + measure := startMemoryMeasurement() |
| 122 | + |
| 123 | + fdfData, err := fdf.LoadFromPath(params.fdfPath) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + f, err := os.Open(params.templatePath) |
| 127 | + require.NoError(t, err) |
| 128 | + defer f.Close() |
| 129 | + |
| 130 | + pdfReader, err := model.NewPdfReader(f) |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + // Populate the form data. |
| 134 | + err = pdfReader.AcroForm.Fill(fdfData) |
| 135 | + require.NoError(t, err) |
| 136 | + |
| 137 | + // Flatten form. |
| 138 | + fieldAppearance := annotator.FieldAppearance{OnlyIfMissing: true, RegenerateTextFields: true} |
| 139 | + |
| 140 | + // NOTE: To customize certain styles try: |
| 141 | + style := fieldAppearance.Style() |
| 142 | + style.CheckmarkRune = '✖' |
| 143 | + style.AutoFontSizeFraction = 0.70 |
| 144 | + fieldAppearance.SetStyle(style) |
| 145 | + |
| 146 | + err = pdfReader.FlattenFields(true, fieldAppearance) |
| 147 | + require.NoError(t, err) |
| 148 | + |
| 149 | + // Write out. |
| 150 | + model.SetPdfProducer("UniDoc") |
| 151 | + pdfWriter := model.NewPdfWriter() |
| 152 | + pdfWriter.SetForms(nil) |
| 153 | + |
| 154 | + for _, p := range pdfReader.PageList { |
| 155 | + err = pdfWriter.AddPage(p) |
| 156 | + require.NoError(t, err) |
| 157 | + } |
| 158 | + |
| 159 | + fout, err := os.Create(params.outPath) |
| 160 | + require.NoError(t, err) |
| 161 | + defer fout.Close() |
| 162 | + |
| 163 | + err = pdfWriter.Write(fout) |
| 164 | + require.NoError(t, err) |
| 165 | + |
| 166 | + measure.Stop() |
| 167 | + summary := measure.Summary() |
| 168 | + t.Logf("%s - summary %s", params.templatePath, summary) |
| 169 | +} |
0 commit comments