Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/optional-cgo' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jerbob92 committed Sep 20, 2023
2 parents 9657d40 + 9d9c1e8 commit 7ad5f91
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 134 deletions.
8 changes: 4 additions & 4 deletions cmd/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ var attachmentsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := pdf.LoadPdfium()
if err != nil {
cmd.PrintErrf("could not load pdfium: %w\n", err)
cmd.PrintErr(fmt.Errorf("could not load pdfium: %w\n", err))
return
}
defer pdf.ClosePdfium()

document, closeFile, err := openFile(args[0])
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not open input file %s: %w\n", args[0], err))
return
}
defer closeFile()
Expand All @@ -59,7 +59,7 @@ var attachmentsCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get attachment count for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get attachment count for PDF %s: %w\n", args[0], err))
return
}

Expand All @@ -68,7 +68,7 @@ var attachmentsCmd = &cobra.Command{
filePath := path.Join(args[1], attachments.Attachments[i].Name)
outFile, err := os.Create(filePath)
if err != nil {
cmd.PrintErrf("could not create output file for attachment %d for PDF %s: %w\n", i, args[0], err)
cmd.PrintErr(fmt.Errorf("could not create output file for attachment %d for PDF %s: %w\n", i, args[0], err))
return
}

Expand Down
14 changes: 7 additions & 7 deletions cmd/explode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ var explodeCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := pdf.LoadPdfium()
if err != nil {
cmd.PrintErrf("could not load pdfium: %w\n", err)
cmd.PrintErr(fmt.Errorf("could not load pdfium: %w\n", err))
return
}
defer pdf.ClosePdfium()

document, closeFile, err := openFile(args[0])
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not open input file %s: %w\n", args[0], err))
return
}
defer closeFile()
Expand All @@ -57,7 +57,7 @@ var explodeCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get page count for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get page count for PDF %s: %w\n", args[0], err))
return
}

Expand All @@ -82,7 +82,7 @@ var explodeCmd = &cobra.Command{
for _, page := range splitPages {
newDocument, err := pdf.PdfiumInstance.FPDF_CreateNewDocument(&requests.FPDF_CreateNewDocument{})
if err != nil {
cmd.PrintErrf("could not create new document for page %s: %w\n", page, err)
cmd.PrintErr(fmt.Errorf("could not create new document for page %s: %w\n", page, err))
return
}

Expand All @@ -97,7 +97,7 @@ var explodeCmd = &cobra.Command{
})
if err != nil {
closeFunc()
cmd.PrintErrf("could not import page %s into new document: %w\n", page, err)
cmd.PrintErr(fmt.Errorf("could not import page %s into new document: %w\n", page, err))
return
}

Expand All @@ -109,7 +109,7 @@ var explodeCmd = &cobra.Command{
} else {
createdFile, err := os.Create(newFilePath)
if err != nil {
cmd.PrintErrf("could not save document for page %s: %w\n", page, err)
cmd.PrintErr(fmt.Errorf("could not save document for page %s: %w\n", page, err))
return
}
fileWriter = createdFile
Expand All @@ -126,7 +126,7 @@ var explodeCmd = &cobra.Command{
})
if err != nil {
closeFunc()
cmd.PrintErrf("could not save document for page %s: %w\n", page, err)
cmd.PrintErr(fmt.Errorf("could not save document for page %s: %w\n", page, err))
return
}

Expand Down
30 changes: 15 additions & 15 deletions cmd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ var imagesCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := pdf.LoadPdfium()
if err != nil {
cmd.PrintErrf("could not load pdfium: %w\n", err)
cmd.PrintErr(fmt.Errorf("could not load pdfium: %w\n", err))
return
}
defer pdf.ClosePdfium()

document, closeFile, err := openFile(args[0])
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not open input file %s: %w\n", args[0], err))
return
}
defer closeFile()
Expand All @@ -74,7 +74,7 @@ var imagesCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get page count for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get page count for PDF %s: %w\n", args[0], err))
return
}

Expand All @@ -85,7 +85,7 @@ var imagesCmd = &cobra.Command{

parsedPageRange, _, err := pdf.NormalizePageRange(pageCount.PageCount, pageRange, false)
if err != nil {
cmd.PrintErrf("invalid page range '%s': %s\n", pageRange, err)
cmd.PrintErr(fmt.Errorf("invalid page range '%s': %s\n", pageRange, err))
return
}

Expand All @@ -98,7 +98,7 @@ var imagesCmd = &cobra.Command{
})

if err != nil {
cmd.PrintErrf("could not load page for page %d for PDF %s: %w\n", pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not load page for page %d for PDF %s: %w\n", pageInt, args[0], err))
return
}

Expand All @@ -116,7 +116,7 @@ var imagesCmd = &cobra.Command{

if err != nil {
closePageFunc()
cmd.PrintErrf("could not get object count for page %d for PDF %s: %w\n", pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get object count for page %d for PDF %s: %w\n", pageInt, args[0], err))
return
}

Expand All @@ -130,7 +130,7 @@ var imagesCmd = &cobra.Command{

if err != nil {
closePageFunc()
cmd.PrintErrf("could not get object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand All @@ -140,7 +140,7 @@ var imagesCmd = &cobra.Command{

if err != nil {
closePageFunc()
cmd.PrintErrf("could not get object type for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get object type for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand All @@ -155,7 +155,7 @@ var imagesCmd = &cobra.Command{

if err != nil {
closePageFunc()
cmd.PrintErrf("could not get image for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get image for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand All @@ -172,7 +172,7 @@ var imagesCmd = &cobra.Command{
if err != nil {
closePageFunc()
closeBitmapFunc()
cmd.PrintErrf("could not get image stride for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get image stride for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand All @@ -183,7 +183,7 @@ var imagesCmd = &cobra.Command{
if err != nil {
closePageFunc()
closeBitmapFunc()
cmd.PrintErrf("could not get image width for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get image width for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand All @@ -194,7 +194,7 @@ var imagesCmd = &cobra.Command{
if err != nil {
closePageFunc()
closeBitmapFunc()
cmd.PrintErrf("could not get image height for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get image height for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand All @@ -205,7 +205,7 @@ var imagesCmd = &cobra.Command{
if err != nil {
closePageFunc()
closeBitmapFunc()
cmd.PrintErrf("could not get image format for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get image format for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand All @@ -216,7 +216,7 @@ var imagesCmd = &cobra.Command{
if err != nil {
closePageFunc()
closeBitmapFunc()
cmd.PrintErrf("could not get image buffer for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get image buffer for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand Down Expand Up @@ -257,7 +257,7 @@ var imagesCmd = &cobra.Command{
if err != nil {
closePageFunc()
closeBitmapFunc()
cmd.PrintErrf("could not create output file for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err)
cmd.PrintErr(fmt.Errorf("could not create output file for object %d for page %d for PDF %s: %w\n", i, pageInt, args[0], err))
return
}

Expand Down
34 changes: 21 additions & 13 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/klippa-app/pdfium-cli/pdf"

"github.com/klippa-app/go-pdfium/requests"
"github.com/klippa-app/go-pdfium/responses"
"github.com/spf13/cobra"
)

Expand All @@ -33,14 +34,14 @@ var infoCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := pdf.LoadPdfium()
if err != nil {
cmd.PrintErrf("could not load pdfium: %w\n", err)
cmd.PrintErr(fmt.Errorf("could not load pdfium: %w\n", err))
return
}
defer pdf.ClosePdfium()

document, closeFile, err := openFile(args[0])
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not open input file %s: %w\n", args[0], err))
return
}

Expand All @@ -52,7 +53,7 @@ var infoCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get version for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get version for PDF %s: %w\n", args[0], err))
return
}

Expand All @@ -77,7 +78,7 @@ var infoCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get page count for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get page count for PDF %s: %w\n", args[0], err))
return
}

Expand All @@ -93,7 +94,7 @@ var infoCmd = &cobra.Command{
},
})
if err != nil {
cmd.PrintErrf("could not get page size for page %d of PDF %s: %w\n", i+1, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get page size for page %d of PDF %s: %w\n", i+1, args[0], err))
return
}

Expand All @@ -113,7 +114,7 @@ var infoCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get permissions for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get permissions for PDF %s: %w\n", args[0], err))
return
}

Expand All @@ -140,7 +141,7 @@ var infoCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get security handler revision for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get security handler revision for PDF %s: %w\n", args[0], err))
return
}

Expand All @@ -154,8 +155,15 @@ var infoCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get signature count for PDF %s: %w\n", args[0], err)
return
// Signatures not enabled in this build.
if isExperimentalError(err) {
signatureCount = &responses.FPDF_GetSignatureCount{
Count: 0,
}
} else {
cmd.PrintErr(fmt.Errorf("could not get signature count for PDF %s: %w\n", args[0], err))
return
}
}

if signatureCount.Count > 0 {
Expand All @@ -166,15 +174,15 @@ var infoCmd = &cobra.Command{
Index: i,
})
if err != nil {
cmd.PrintErrf("could not get signature object %d for PDF %s: %w\n", i, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get signature object %d for PDF %s: %w\n", i, args[0], err))
return
}

signatureTime, err := pdf.PdfiumInstance.FPDFSignatureObj_GetTime(&requests.FPDFSignatureObj_GetTime{
Signature: signatureObj.Signature,
})
if err != nil {
cmd.PrintErrf("could not get signature reason for signature object %d for PDF %s: %w\n", i, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get signature reason for signature object %d for PDF %s: %w\n", i, args[0], err))
return
}
time := ""
Expand All @@ -186,7 +194,7 @@ var infoCmd = &cobra.Command{
Signature: signatureObj.Signature,
})
if err != nil {
cmd.PrintErrf("could not get signature reason for signature object %d for PDF %s: %w\n", i, args[0], err)
cmd.PrintErr(fmt.Errorf("could not get signature reason for signature object %d for PDF %s: %w\n", i, args[0], err))
return
}

Expand All @@ -203,7 +211,7 @@ var infoCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get signature count for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get signature count for PDF %s: %w\n", args[0], err))
return
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/javascripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ var javascriptsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := pdf.LoadPdfium()
if err != nil {
cmd.PrintErrf("could not load pdfium: %w\n", err)
cmd.PrintErr(fmt.Errorf("could not load pdfium: %w\n", err))
return
}
defer pdf.ClosePdfium()

document, closeFile, err := openFile(args[0])
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not open input file %s: %w\n", args[0], err))
return
}
defer closeFile()
Expand All @@ -59,7 +59,7 @@ var javascriptsCmd = &cobra.Command{
Document: document.Document,
})
if err != nil {
cmd.PrintErrf("could not get javascript count for PDF %s: %w\n", args[0], err)
cmd.PrintErr(fmt.Errorf("could not get javascript count for PDF %s: %w\n", args[0], err))
return
}

Expand All @@ -68,7 +68,7 @@ var javascriptsCmd = &cobra.Command{
filePath := path.Join(args[1], fmt.Sprintf("%s.js", javascripts.JavaScriptActions[i].Name))
outFile, err := os.Create(filePath)
if err != nil {
cmd.PrintErrf("could not create output file for javascript %d for PDF %s: %w\n", i, args[0], err)
cmd.PrintErr(fmt.Errorf("could not create output file for javascript %d for PDF %s: %w\n", i, args[0], err))
return
}

Expand Down
Loading

0 comments on commit 7ad5f91

Please sign in to comment.