Skip to content

Commit

Permalink
Merge branch 'main' into B-21655-Alternative-AK_HI-Duty-Location-Names
Browse files Browse the repository at this point in the history
  • Loading branch information
r-mettler authored Nov 15, 2024
2 parents 2457bf9 + 23e12ea commit ae6914f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Binary file modified pkg/assets/paperwork/formtemplates/ShipmentSummaryWorksheet.pdf
Binary file not shown.
40 changes: 40 additions & 0 deletions pkg/paperwork/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,46 @@ func (g *Generator) FillPDFForm(jsonData []byte, templateReader io.ReadSeeker, f
return outputFile, nil
}

// LockPDFForm takes in a PDF Form readseeker, reads all form fields, and locks them
// This is primarily for the SSW, but needs to be done separately from filling as only one process (filling, locking, merging, etc)
// may be completed at a time.
func (g *Generator) LockPDFForm(templateReader io.ReadSeeker, fileName string) (SSWWorksheet afero.File, err error) {
var conf = g.pdfConfig
buf := new(bytes.Buffer)
// Reads all form fields on document as []form.Field
fields, err := api.FormFields(templateReader, conf)
// Assembles them to the API's required []string
fieldList := make([]string, len(fields))
for i, field := range fields {
fieldList[i] = field.ID
}

// Locks all fields
formerr := api.LockFormFields(templateReader, buf, fieldList, conf)
if formerr != nil {
return nil, err
}

tempFile, err := g.newTempFileWithName(fileName) // Will use g.newTempFileWithName for proper memory usage, saves the new temp file with the fileName
if err != nil {
return nil, err
}

// copy byte[] to temp file
_, err = io.Copy(tempFile, buf)
if err != nil {
return nil, errors.Wrap(err, "error io.Copy on byte[] to temp")
}

// Reload the file from memstore
outputFile, err := g.FileSystem().Open(tempFile.Name())
if err != nil {
return nil, errors.Wrap(err, "error g.fs.Open on reload from memstore")
}

return outputFile, nil
}

// MergePDFFiles Merges a slice of paths to PDF files into a single PDF
func (g *Generator) MergePDFFilesByContents(_ appcontext.AppContext, fileReaders []io.ReadSeeker) (afero.File, error) {
var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ func (SSWPPMGenerator *SSWPPMGenerator) FillSSWPDFForm(Page1Values services.Page
var sswHeader = header{
Source: "ShipmentSummaryWorksheet.pdf",
Version: "pdfcpu v0.9.1 dev",
Creation: "2024-10-29 20:00:40 UTC",
Creation: "2024-11-13 13:44:05 UTC",
Producer: "macOS Version 13.5 (Build 22G74) Quartz PDFContext, AppendMode 1.1",
}

Expand Down Expand Up @@ -1185,6 +1185,11 @@ func (SSWPPMGenerator *SSWPPMGenerator) FillSSWPDFForm(Page1Values services.Page
return nil, nil, err
}

SSWWorksheet, err = SSWPPMGenerator.generator.LockPDFForm(SSWWorksheet, "")
if err != nil {
return nil, nil, err
}

// pdfInfo.PageCount is a great way to tell whether returned PDF is corrupted. Pages is expected pages
const pages = 3
pdfInfoResult, err := SSWPPMGenerator.generator.GetPdfFileInfo(SSWWorksheet.Name())
Expand Down

0 comments on commit ae6914f

Please sign in to comment.