feat: use new pdf lib
This commit is contained in:
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
.idea
|
.idea
|
||||||
|
dist
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
GOARCH=amd64 GOOS=windows go build -ldflags "-s -w" -o dist/pdf_xml_helper.exe main.go
|
||||||
@@ -18,7 +18,9 @@ require (
|
|||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 // indirect
|
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 // indirect
|
||||||
|
gitlab.com/romalor/pdflib v0.2.2 // indirect
|
||||||
golang.org/x/crypto v0.46.0 // indirect
|
golang.org/x/crypto v0.46.0 // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93 // indirect
|
||||||
golang.org/x/image v0.34.0 // indirect
|
golang.org/x/image v0.34.0 // indirect
|
||||||
golang.org/x/text v0.32.0 // indirect
|
golang.org/x/text v0.32.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
|||||||
@@ -22,8 +22,12 @@ github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
|
|||||||
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
|
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
|
||||||
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 h1:FnBeRrxr7OU4VvAzt5X7s6266i6cSVkkFPS0TuXWbIg=
|
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 h1:FnBeRrxr7OU4VvAzt5X7s6266i6cSVkkFPS0TuXWbIg=
|
||||||
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
|
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
|
||||||
|
gitlab.com/romalor/pdflib v0.2.2 h1:gbwkCS/371jnMqnDmJkj9MzwdZ+PhhzsbDK6StvcA7w=
|
||||||
|
gitlab.com/romalor/pdflib v0.2.2/go.mod h1:BIibw9gBIyPcSF4BJrJfjGdL+orRKVDqJFJ9OGZ4+UM=
|
||||||
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
||||||
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
||||||
|
golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93 h1:fQsdNF2N+/YewlRZiricy4P1iimyPKZ/xwniHj8Q2a0=
|
||||||
|
golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93/go.mod h1:EPRbTFwzwjXj9NpYyyrvenVh9Y+GFeEvMNh7Xuz7xgU=
|
||||||
golang.org/x/image v0.34.0 h1:33gCkyw9hmwbZJeZkct8XyR11yH889EQt/QH4VmXMn8=
|
golang.org/x/image v0.34.0 h1:33gCkyw9hmwbZJeZkct8XyR11yH889EQt/QH4VmXMn8=
|
||||||
golang.org/x/image v0.34.0/go.mod h1:2RNFBZRB+vnwwFil8GkMdRvrJOFd1AzdZI6vOY+eJVU=
|
golang.org/x/image v0.34.0/go.mod h1:2RNFBZRB+vnwwFil8GkMdRvrJOFd1AzdZI6vOY+eJVU=
|
||||||
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
|
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
|
||||||
|
|||||||
Executable → Regular
+42
-32
@@ -1,19 +1,25 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/pdfcpu/pdfcpu/pkg/api"
|
"gitlab.com/romalor/pdflib/pkg/components/attachment"
|
||||||
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
|
"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 := GetDebugLogger()
|
||||||
logger.Log(fmt.Sprintf("enter appendToPdf(%s, %s, %s)", inputPdfFilePath, inputXmlFilePath, outputFilePath))
|
logger.Log(fmt.Sprintf("enter appendToPdf(%s, %s, %s)", inputPdfFilePath, inputXmlFilePath, outputFilePath))
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -35,6 +41,14 @@ func AppendToPdf(inputPdfFilePath string, inputXmlFilePath string, outputFilePat
|
|||||||
) + "_xml.pdf"
|
) + "_xml.pdf"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if description == "" {
|
||||||
|
description = "Rechnung"
|
||||||
|
}
|
||||||
|
|
||||||
|
if embedName == "" {
|
||||||
|
embedName = "factur-x.xml"
|
||||||
|
}
|
||||||
|
|
||||||
logger.Log(
|
logger.Log(
|
||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"pdfInput: '%s' xmlInput: '%s' output: '%s'",
|
"pdfInput: '%s' xmlInput: '%s' output: '%s'",
|
||||||
@@ -50,48 +64,44 @@ func AppendToPdf(inputPdfFilePath string, inputXmlFilePath string, outputFilePat
|
|||||||
}
|
}
|
||||||
defer inputFile.Close()
|
defer inputFile.Close()
|
||||||
|
|
||||||
dest := bytes.Buffer{}
|
var doc *pdf.Document
|
||||||
|
doc, err = pdf.OpenReaderWithOptions(inputFile, pdf.OpenOptions{
|
||||||
err = api.AddAttachments(
|
Writable: true,
|
||||||
inputFile,
|
})
|
||||||
&dest,
|
|
||||||
[]string{inputXmlFilePath},
|
|
||||||
true,
|
|
||||||
model.NewDefaultConfiguration(),
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
source := bytes.NewReader(dest.Bytes())
|
var inputXmlFile *os.File
|
||||||
dest = bytes.Buffer{}
|
inputXmlFile, err = os.Open(inputXmlFilePath)
|
||||||
|
|
||||||
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())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var outputFile *os.File
|
var inputData []byte
|
||||||
outputFile, err = os.Create(outputFilePath)
|
inputData, err = io.ReadAll(inputXmlFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-30
@@ -3,13 +3,12 @@ package lib
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pdfcpu/pdfcpu/pkg/api"
|
attachment2 "gitlab.com/romalor/pdflib/pkg/components/attachment"
|
||||||
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
|
"gitlab.com/romalor/pdflib/pkg/pdf"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExtractFromPdf(inputFilePath string, outputFilePath string) error {
|
func ExtractFromPdf(inputFilePath string, outputFilePath string) error {
|
||||||
@@ -35,43 +34,24 @@ func ExtractFromPdf(inputFilePath string, outputFilePath string) error {
|
|||||||
}
|
}
|
||||||
defer inputFile.Close()
|
defer inputFile.Close()
|
||||||
|
|
||||||
var (
|
var attachments []attachment2.Attachment
|
||||||
attachments []model.Attachment
|
|
||||||
rawData []byte
|
|
||||||
ctx *model.Context
|
|
||||||
attachment *model.Attachment
|
|
||||||
)
|
|
||||||
|
|
||||||
ctx, err = api.ReadContextFile(inputFilePath)
|
var doc *pdf.Document
|
||||||
if err != nil {
|
doc, err = pdf.OpenReader(inputFile)
|
||||||
return err
|
|
||||||
}
|
|
||||||
attachments, err = api.Attachments(inputFile, model.NewDefaultConfiguration())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
attachment, err = ctx.ExtractAttachment(attachments[0])
|
attachments, err = doc.Attachments()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
rawData, err = io.ReadAll(attachment)
|
if len(attachments) == 0 {
|
||||||
if err != nil {
|
return errors.New("no attachments found")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var outputFile *os.File
|
err = doc.ExtractAttachment(attachments[0].Name, outputFilePath)
|
||||||
outputFile, err = os.Create(outputFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer outputFile.Close()
|
|
||||||
|
|
||||||
_, err = outputFile.Write(rawData)
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
|
||||||
"pdf/lib"
|
"pdf/lib"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -57,16 +58,28 @@ func main() {
|
|||||||
Usage: "Debug flag",
|
Usage: "Debug flag",
|
||||||
Aliases: []string{"d"},
|
Aliases: []string{"d"},
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "description",
|
||||||
|
Usage: "Description for the embedded xml file",
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "embedded-name",
|
||||||
|
Usage: "Name of the xml file inside the pdf",
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: func(context *cli.Context) error {
|
Action: func(context *cli.Context) error {
|
||||||
inputPdfFilePath := context.Args().Get(0)
|
inputPdfFilePath := context.Args().Get(0)
|
||||||
inputXmlFilePath := context.String("xml-input")
|
inputXmlFilePath := context.String("xml-input")
|
||||||
outputFilePath := context.String("out")
|
outputFilePath := context.String("out")
|
||||||
|
description := context.String("description")
|
||||||
|
embedName := context.String("embedded-name")
|
||||||
debug := context.Bool("debug")
|
debug := context.Bool("debug")
|
||||||
|
|
||||||
lib.EnableDebug(debug)
|
lib.EnableDebug(debug)
|
||||||
|
|
||||||
lib.AppendToPdf(inputPdfFilePath, inputXmlFilePath, outputFilePath)
|
lib.AppendToPdf(inputPdfFilePath, inputXmlFilePath, outputFilePath, embedName, description)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user