feat: use new pdf lib
This commit is contained in:
+10
-30
@@ -3,13 +3,12 @@ package lib
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pdfcpu/pdfcpu/pkg/api"
|
||||
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
|
||||
attachment2 "gitlab.com/romalor/pdflib/pkg/components/attachment"
|
||||
"gitlab.com/romalor/pdflib/pkg/pdf"
|
||||
)
|
||||
|
||||
func ExtractFromPdf(inputFilePath string, outputFilePath string) error {
|
||||
@@ -35,43 +34,24 @@ func ExtractFromPdf(inputFilePath string, outputFilePath string) error {
|
||||
}
|
||||
defer inputFile.Close()
|
||||
|
||||
var (
|
||||
attachments []model.Attachment
|
||||
rawData []byte
|
||||
ctx *model.Context
|
||||
attachment *model.Attachment
|
||||
)
|
||||
var attachments []attachment2.Attachment
|
||||
|
||||
ctx, err = api.ReadContextFile(inputFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attachments, err = api.Attachments(inputFile, model.NewDefaultConfiguration())
|
||||
var doc *pdf.Document
|
||||
doc, err = pdf.OpenReader(inputFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
attachment, err = ctx.ExtractAttachment(attachments[0])
|
||||
attachments, err = doc.Attachments()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rawData, err = io.ReadAll(attachment)
|
||||
if err != nil {
|
||||
return err
|
||||
if len(attachments) == 0 {
|
||||
return errors.New("no attachments found")
|
||||
}
|
||||
|
||||
var outputFile *os.File
|
||||
outputFile, err = os.Create(outputFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outputFile.Close()
|
||||
err = doc.ExtractAttachment(attachments[0].Name, outputFilePath)
|
||||
|
||||
_, err = outputFile.Write(rawData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user