Skip to content

Commit

Permalink
Add support for piping
Browse files Browse the repository at this point in the history
  • Loading branch information
jerbob92 committed Sep 19, 2023
1 parent a570324 commit 59be9cf
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 140 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Extracting attachments from PDFs
* Extracting thumbnails from PDFs
* Extracting JavaScripts from PDFs
* Piping input through stdin when the input is one file
* Piping output through stdout when the output is one file

## PDFium & Wazero

Expand Down
15 changes: 4 additions & 11 deletions cmd/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func init() {
var attachmentsCmd = &cobra.Command{
Use: "attachments [input] [output-folder]",
Short: "Extract the attachments of a PDF",
Long: "Extract the attachments of a PDF and store them as file.",
Long: "Extract the attachments of a PDF and store them as file.\n[input] can either be a file path or - for stdin.",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(2)(cmd, args); err != nil {
return err
}

if _, err := os.Stat(args[0]); err != nil {
if err := validFile(args[0]); err != nil {
return fmt.Errorf("could not open input file %s: %w\n", args[0], err)
}

Expand All @@ -48,19 +48,12 @@ var attachmentsCmd = &cobra.Command{
}
defer pdf.ClosePdfium()

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

document, err := openFile(file)
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
return
}
defer pdf.PdfiumInstance.FPDF_CloseDocument(&requests.FPDF_CloseDocument{Document: document.Document})
defer closeFile()

attachments, err := pdf.PdfiumInstance.GetAttachments(&requests.GetAttachments{
Document: document.Document,
Expand Down
52 changes: 32 additions & 20 deletions cmd/explode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io"
"os"
"strings"

Expand All @@ -21,17 +22,17 @@ func init() {
var explodeCmd = &cobra.Command{
Use: "explode [input] [output]",
Short: "Explode a PDF into multiple PDFs",
Long: "Explode a PDF into multiple PDFs, the output filename should contain a \"%d\" placeholder for the page number, e.g. split invoice.pdf invoice-%d.pdf, the result for a 2-page PDF will be invoice-1.pdf and invoice-2.pdf.",
Long: "Explode a PDF into multiple PDFs.\n[input] can either be a file path or - for stdin.\n[output] can either be a file path or - for stdout, stdout is only supported when the output will be one file. The output filename should contain a \"%d\" placeholder for the page number, e.g. split invoice.pdf invoice-%d.pdf, the result for a 2-page PDF will be invoice-1.pdf and invoice-2.pdf.",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(2)(cmd, args); err != nil {
return err
}

if _, err := os.Stat(args[0]); err != nil {
if err := validFile(args[0]); err != nil {
return fmt.Errorf("could not open input file %s: %w\n", args[0], err)
}

if !strings.Contains(args[1], "%d") {
if args[1] != stdFilename && !strings.Contains(args[1], "%d") {
return fmt.Errorf("output string %s should contain page pattern %%d\n", args[1])
}

Expand All @@ -45,19 +46,12 @@ var explodeCmd = &cobra.Command{
}
defer pdf.ClosePdfium()

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

document, err := openFile(file)
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
return
}
defer pdf.PdfiumInstance.FPDF_CloseDocument(&requests.FPDF_CloseDocument{Document: document.Document})
defer closeFile()

pageCount, err := pdf.PdfiumInstance.FPDF_GetPageCount(&requests.FPDF_GetPageCount{
Document: document.Document,
Expand All @@ -79,6 +73,12 @@ var explodeCmd = &cobra.Command{
}

splitPages := strings.Split(*parsedPageRange, ",")

if args[1] == stdFilename && len(splitPages) > 1 {
cmd.PrintErrf("could not explode into multiple pages with output to stdout\n")
return
}

for _, page := range splitPages {
newDocument, err := pdf.PdfiumInstance.FPDF_CreateNewDocument(&requests.FPDF_CreateNewDocument{})
if err != nil {
Expand All @@ -102,27 +102,39 @@ var explodeCmd = &cobra.Command{
}

newFilePath := strings.Replace(args[1], "%d", page, -1)
createdFile, err := os.Create(newFilePath)
if err != nil {
cmd.PrintErrf("could not save document for page %s: %w\n", page, err)
return

var fileWriter io.Writer
if args[1] == stdFilename {
fileWriter = os.Stdout
} else {
createdFile, err := os.Create(newFilePath)
if err != nil {
cmd.PrintErrf("could not save document for page %s: %w\n", page, err)
return
}
fileWriter = createdFile
originalCloseFunc := closeFunc
closeFunc = func() {
originalCloseFunc()
createdFile.Close()
}
}

_, err = pdf.PdfiumInstance.FPDF_SaveAsCopy(&requests.FPDF_SaveAsCopy{
Document: newDocument.Document,
FileWriter: createdFile,
FileWriter: fileWriter,
})
if err != nil {
closeFunc()
createdFile.Close()
cmd.PrintErrf("could not save document for page %s: %w\n", page, err)
return
}

closeFunc()
createdFile.Close()

cmd.Printf("Exploded page %s into %s\n", page, newFilePath)
if args[1] != stdFilename {
cmd.Printf("Exploded page %s into %s\n", page, newFilePath)
}
}
},
}
15 changes: 4 additions & 11 deletions cmd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func init() {
var imagesCmd = &cobra.Command{
Use: "images [input] [output-folder]",
Short: "Extract the images of a PDF",
Long: "Extract the images of a PDF and store them as file.",
Long: "Extract the images of a PDF and store them as file.\n[input] can either be a file path or - for stdin.",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(2)(cmd, args); err != nil {
return err
}

if _, err := os.Stat(args[0]); err != nil {
if err := validFile(args[0]); err != nil {
return fmt.Errorf("could not open input file %s: %w\n", args[0], err)
}

Expand All @@ -63,19 +63,12 @@ var imagesCmd = &cobra.Command{
}
defer pdf.ClosePdfium()

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

document, err := openFile(file)
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
return
}
defer pdf.PdfiumInstance.FPDF_CloseDocument(&requests.FPDF_CloseDocument{Document: document.Document})
defer closeFile()

pageCount, err := pdf.PdfiumInstance.FPDF_GetPageCount(&requests.FPDF_GetPageCount{
Document: document.Document,
Expand Down
13 changes: 4 additions & 9 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/klippa-app/pdfium-cli/pdf"
Expand All @@ -25,7 +24,7 @@ var infoCmd = &cobra.Command{
return err
}

if _, err := os.Stat(args[0]); err != nil {
if err := validFile(args[0]); err != nil {
return fmt.Errorf("could not open input file %s: %w\n", args[0], err)
}

Expand All @@ -39,18 +38,14 @@ var infoCmd = &cobra.Command{
}
defer pdf.ClosePdfium()

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

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

defer pdf.PdfiumInstance.FPDF_CloseDocument(&requests.FPDF_CloseDocument{Document: document.Document})

fileVersion, err := pdf.PdfiumInstance.FPDF_GetFileVersion(&requests.FPDF_GetFileVersion{
Expand Down
15 changes: 4 additions & 11 deletions cmd/javascripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func init() {
var javascriptsCmd = &cobra.Command{
Use: "javascripts [input] [output-folder]",
Short: "Extract the javascripts of a PDF",
Long: "Extract the javascripts of a PDF and store them as file.",
Long: "Extract the javascripts of a PDF and store them as file.\n[input] can either be a file path or - for stdin.",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(2)(cmd, args); err != nil {
return err
}

if _, err := os.Stat(args[0]); err != nil {
if err := validFile(args[0]); err != nil {
return fmt.Errorf("could not open input file %s: %w\n", args[0], err)
}

Expand All @@ -48,19 +48,12 @@ var javascriptsCmd = &cobra.Command{
}
defer pdf.ClosePdfium()

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

document, err := openFile(file)
if err != nil {
cmd.PrintErrf("could not open input file %s: %w\n", args[0], err)
return
}
defer pdf.PdfiumInstance.FPDF_CloseDocument(&requests.FPDF_CloseDocument{Document: document.Document})
defer closeFile()

javascripts, err := pdf.PdfiumInstance.GetJavaScriptActions(&requests.GetJavaScriptActions{
Document: document.Document,
Expand Down
34 changes: 17 additions & 17 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io"
"os"

"github.com/klippa-app/go-pdfium/requests"
Expand All @@ -18,7 +19,7 @@ func init() {
var mergeCmd = &cobra.Command{
Use: "merge [input] [input] ([input]...) [output]",
Short: "Merge multiple PDFs into a single PDF",
Long: "Merge multiple PDFs into a single PDF",
Long: "Merge multiple PDFs into a single PDF.\n[output] can either be a file path or - for stdout.",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.MinimumNArgs(3)(cmd, args); err != nil {
return err
Expand Down Expand Up @@ -48,21 +49,14 @@ var mergeCmd = &cobra.Command{

mergedPageCount := 0
for i := 0; i < len(args)-1; i++ {
file, err := os.Open(args[i])
if err != nil {
cmd.PrintErrf("could not open input file %s: %w", args[i], err)
return
}
defer file.Close()

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

closeFunc := func() {
pdf.PdfiumInstance.FPDF_CloseDocument(&requests.FPDF_CloseDocument{Document: newDocument.Document})
closeFile()
}

pageCount, err := pdf.PdfiumInstance.FPDF_GetPageCount(&requests.FPDF_GetPageCount{
Expand Down Expand Up @@ -107,17 +101,23 @@ var mergeCmd = &cobra.Command{
closeFunc()
}

createdFile, err := os.Create(args[len(args)-1])
if err != nil {
cmd.PrintErrf("could not save document: %w", err)
return
}
var fileWriter io.Writer
if args[len(args)-1] == stdFilename {
fileWriter = os.Stdout
} else {
createdFile, err := os.Create(args[len(args)-1])
if err != nil {
cmd.PrintErrf("could not save document: %w", err)
return
}

defer createdFile.Close()
defer createdFile.Close()
fileWriter = createdFile
}

_, err = pdf.PdfiumInstance.FPDF_SaveAsCopy(&requests.FPDF_SaveAsCopy{
Document: newDocument.Document,
FileWriter: createdFile,
FileWriter: fileWriter,
})
if err != nil {
cmd.PrintErrf("could not save new document %s: %w", err)
Expand Down
Loading

0 comments on commit 59be9cf

Please sign in to comment.