feat: use new pdf lib
This commit is contained in:
+42
-32
@@ -1,19 +1,25 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pdfcpu/pdfcpu/pkg/api"
|
||||
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
|
||||
"gitlab.com/romalor/pdflib/pkg/components/attachment"
|
||||
"gitlab.com/romalor/pdflib/pkg/pdf"
|
||||
)
|
||||
|
||||
func AppendToPdf(inputPdfFilePath string, inputXmlFilePath string, outputFilePath string) error {
|
||||
func AppendToPdf(
|
||||
inputPdfFilePath string,
|
||||
inputXmlFilePath string,
|
||||
outputFilePath string,
|
||||
embedName string,
|
||||
description string,
|
||||
) error {
|
||||
logger := GetDebugLogger()
|
||||
logger.Log(fmt.Sprintf("enter appendToPdf(%s, %s, %s)", inputPdfFilePath, inputXmlFilePath, outputFilePath))
|
||||
defer func() {
|
||||
@@ -35,6 +41,14 @@ func AppendToPdf(inputPdfFilePath string, inputXmlFilePath string, outputFilePat
|
||||
) + "_xml.pdf"
|
||||
}
|
||||
|
||||
if description == "" {
|
||||
description = "Rechnung"
|
||||
}
|
||||
|
||||
if embedName == "" {
|
||||
embedName = "factur-x.xml"
|
||||
}
|
||||
|
||||
logger.Log(
|
||||
fmt.Sprintf(
|
||||
"pdfInput: '%s' xmlInput: '%s' output: '%s'",
|
||||
@@ -50,48 +64,44 @@ func AppendToPdf(inputPdfFilePath string, inputXmlFilePath string, outputFilePat
|
||||
}
|
||||
defer inputFile.Close()
|
||||
|
||||
dest := bytes.Buffer{}
|
||||
|
||||
err = api.AddAttachments(
|
||||
inputFile,
|
||||
&dest,
|
||||
[]string{inputXmlFilePath},
|
||||
true,
|
||||
model.NewDefaultConfiguration(),
|
||||
)
|
||||
var doc *pdf.Document
|
||||
doc, err = pdf.OpenReaderWithOptions(inputFile, pdf.OpenOptions{
|
||||
Writable: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
source := bytes.NewReader(dest.Bytes())
|
||||
dest = bytes.Buffer{}
|
||||
|
||||
err = api.AddProperties(source, &dest, map[string]string{
|
||||
"DocumentFileName": path.Base(inputXmlFilePath),
|
||||
"DocumentType": "INVOICE",
|
||||
"ConformanceLevel": "EN 16931",
|
||||
"SchemasSchema": "Factur-X PDFA Extension Schema",
|
||||
"SchemasPrefix": "fx",
|
||||
"SchemasPropertyName": "DocumentFileName",
|
||||
"SchemasPropertyValueType": "Text",
|
||||
"SchemasPropertyCategory": "external",
|
||||
"SchemasPropertyDescription": "The name of the embedded XML document",
|
||||
}, model.NewDefaultConfiguration())
|
||||
var inputXmlFile *os.File
|
||||
inputXmlFile, err = os.Open(inputXmlFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var outputFile *os.File
|
||||
outputFile, err = os.Create(outputFilePath)
|
||||
var inputData []byte
|
||||
inputData, err = io.ReadAll(inputXmlFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
_, err = outputFile.Write(dest.Bytes())
|
||||
err = doc.AddAttachment(attachment.Attachment{
|
||||
Name: embedName,
|
||||
Description: description,
|
||||
MIMEType: "text/xml",
|
||||
Size: int64(len(inputData)),
|
||||
Relationship: "Alternative",
|
||||
ModDate: time.Now(),
|
||||
}, inputData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Log(doc.Writer().Version)
|
||||
err = doc.Writer().SaveAs(outputFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user