initial commit

This commit is contained in:
2025-12-23 12:17:27 +01:00
commit 8b73c86796
21 changed files with 2279 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
package lib
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
)
func AppendToPdf(inputPdfFilePath string, inputXmlFilePath string, outputFilePath string) error {
logger := GetDebugLogger()
logger.Log(fmt.Sprintf("enter appendToPdf(%s, %s, %s)", inputPdfFilePath, inputXmlFilePath, outputFilePath))
defer func() {
logger.Log("leave appendToPdf")
}()
if inputPdfFilePath == "" {
return errors.New("no input pdf file specified")
}
if inputXmlFilePath == "" {
inputXmlFilePath = strings.TrimSuffix(inputPdfFilePath, filepath.Ext(inputPdfFilePath)) + ".xml"
}
if outputFilePath == "" {
outputFilePath = strings.TrimSuffix(
inputPdfFilePath,
filepath.Ext(inputPdfFilePath),
) + "_xml.pdf"
}
logger.Log(
fmt.Sprintf(
"pdfInput: '%s' xmlInput: '%s' output: '%s'",
inputPdfFilePath,
inputXmlFilePath,
outputFilePath,
),
)
err := api.AddAttachmentsFile(
inputPdfFilePath,
outputFilePath,
[]string{inputXmlFilePath},
true,
model.NewDefaultConfiguration(),
)
if err != nil {
return err
}
return nil
}
+62
View File
@@ -0,0 +1,62 @@
package lib
import (
"fmt"
"io"
"os"
"runtime"
"time"
)
var debugEnabled = false
var debugFile = "debug.log"
var debugLogger *DebugLogger
type DebugLogger struct {
file *os.File
}
func EnableDebug(enable bool) {
debugEnabled = enable
}
func GetDebugLogger() *DebugLogger {
if debugLogger == nil {
debugLogger = &DebugLogger{}
runtime.SetFinalizer(
debugLogger, func(logger *DebugLogger) {
logger.Close()
},
)
if debugEnabled {
var err error
_, err = os.Stat(debugFile)
if err != nil {
debugLogger.file, _ = os.Create(debugFile)
} else {
debugLogger.file, _ = os.OpenFile(debugFile, os.O_RDWR, 0644)
debugLogger.file.Seek(0, io.SeekEnd)
}
}
}
return debugLogger
}
func (l *DebugLogger) Log(msg ...any) {
if l.file == nil {
return
}
l.file.WriteString(fmt.Sprintln(append([]any{time.Now().Format("2006-01-02 15:04:05")}, msg...)...))
}
func (l *DebugLogger) Close() error {
if l.file == nil {
return nil
}
return l.file.Close()
}
+77
View File
@@ -0,0 +1,77 @@
package lib
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
)
func ExtractFromPdf(inputFilePath string, outputFilePath string) error {
logger := GetDebugLogger()
logger.Log(fmt.Sprintf("enter ExtractFromPdf(%s, %s)", inputFilePath, outputFilePath))
defer func() {
logger.Log("leave extractFromPdf")
}()
if inputFilePath == "" {
return errors.New("no input file specified")
}
if outputFilePath == "" {
outputFilePath = strings.TrimSuffix(inputFilePath, filepath.Ext(inputFilePath)) + ".xml"
}
logger.Log(fmt.Sprintf("pdfInput: '%s' output: '%s'", inputFilePath, outputFilePath))
inputFile, err := os.Open(inputFilePath)
if err != nil {
return err
}
defer inputFile.Close()
var (
attachments []model.Attachment
rawData []byte
ctx *model.Context
attachment *model.Attachment
)
ctx, err = api.ReadContextFile(inputFilePath)
if err != nil {
return err
}
attachments, err = api.Attachments(inputFile, model.NewDefaultConfiguration())
if err != nil {
return err
}
attachment, err = ctx.ExtractAttachment(attachments[0])
if err != nil {
return err
}
rawData, err = io.ReadAll(attachment)
if err != nil {
return err
}
var outputFile *os.File
outputFile, err = os.Create(outputFilePath)
if err != nil {
return err
}
defer outputFile.Close()
_, err = outputFile.Write(rawData)
if err != nil {
return err
}
return nil
}