mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-13 02:42:14 +01:00
feat(pdfengines): add PDF/UA (#714)
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
// Package pdfcpu provides a module which wraps the
|
||||
// https://github.com/pdfcpu/pdfcpu library and implements the
|
||||
// gotenberg.PDFEngine interface.
|
||||
// Package pdfcpu provides an implementation of the gotenberg.PdfEngine
|
||||
// interface using the pdfcpu library. This package allows for the merging of
|
||||
// PDF files but does not support conversion to specific PDF formats.
|
||||
//
|
||||
// See: https://github.com/pdfcpu/pdfcpu.
|
||||
package pdfcpu
|
||||
|
||||
@@ -13,35 +13,34 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
gotenberg.MustRegisterModule(PDFcpu{})
|
||||
gotenberg.MustRegisterModule(new(PdfCpu))
|
||||
}
|
||||
|
||||
// PDFcpu is a module which wraps the https://github.com/pdfcpu/pdfcpu library
|
||||
// and implements the gotenberg.PDFEngine interface.
|
||||
type PDFcpu struct {
|
||||
// PdfCpu abstracts the pdfcpu library and implements the [gotenberg.PdfEngine]
|
||||
// interface.
|
||||
type PdfCpu struct {
|
||||
conf *pdfcpuConfig.Configuration
|
||||
}
|
||||
|
||||
// Descriptor returns a PDFcpu's module descriptor.
|
||||
func (PDFcpu) Descriptor() gotenberg.ModuleDescriptor {
|
||||
// Descriptor returns a [PdfCpu]'s module descriptor.
|
||||
func (engine *PdfCpu) Descriptor() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{
|
||||
ID: "pdfcpu",
|
||||
New: func() gotenberg.Module { return new(PDFcpu) },
|
||||
New: func() gotenberg.Module { return new(PdfCpu) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets the engine properties.
|
||||
func (engine *PDFcpu) Provision(_ *gotenberg.Context) error {
|
||||
func (engine *PdfCpu) Provision(ctx *gotenberg.Context) error {
|
||||
pdfcpuConfig.ConfigPath = "disable"
|
||||
pdfcpuLog.DisableLoggers()
|
||||
|
||||
engine.conf = pdfcpuConfig.NewDefaultConfiguration()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Merge merges the given PDFs into a unique Pdf.
|
||||
func (engine PDFcpu) Merge(_ context.Context, _ *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
// Merge combines multiple PDFs into a single PDF.
|
||||
func (engine *PdfCpu) Merge(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||
err := pdfcpuAPI.MergeCreateFile(inputPaths, outputPath, engine.conf)
|
||||
if err == nil {
|
||||
return nil
|
||||
@@ -50,14 +49,14 @@ func (engine PDFcpu) Merge(_ context.Context, _ *zap.Logger, inputPaths []string
|
||||
return fmt.Errorf("merge PDFs with PDFcpu: %w", err)
|
||||
}
|
||||
|
||||
// Convert is not available for this Pdf engine.
|
||||
func (engine PDFcpu) Convert(_ context.Context, _ *zap.Logger, format, _, _ string) error {
|
||||
return fmt.Errorf("convert Pdf to '%s' with PDFcpu: %w", format, gotenberg.ErrPDFEngineMethodNotAvailable)
|
||||
// Convert is not available in this implementation.
|
||||
func (engine *PdfCpu) Convert(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error {
|
||||
return fmt.Errorf("convert PDF to '%+v' with PDFcpu: %w", formats, gotenberg.ErrPdfEngineMethodNotSupported)
|
||||
}
|
||||
|
||||
// Interface guards.
|
||||
var (
|
||||
_ gotenberg.Module = (*PDFcpu)(nil)
|
||||
_ gotenberg.Provisioner = (*PDFcpu)(nil)
|
||||
_ gotenberg.PDFEngine = (*PDFcpu)(nil)
|
||||
_ gotenberg.Module = (*PdfCpu)(nil)
|
||||
_ gotenberg.Provisioner = (*PdfCpu)(nil)
|
||||
_ gotenberg.PdfEngine = (*PdfCpu)(nil)
|
||||
)
|
||||
|
||||
@@ -12,89 +12,93 @@ import (
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
||||
)
|
||||
|
||||
func TestPDFcpu_Descriptor(t *testing.T) {
|
||||
descriptor := PDFcpu{}.Descriptor()
|
||||
func TestPdfCpu_Descriptor(t *testing.T) {
|
||||
descriptor := new(PdfCpu).Descriptor()
|
||||
|
||||
actual := reflect.TypeOf(descriptor.New())
|
||||
expect := reflect.TypeOf(new(PDFcpu))
|
||||
expect := reflect.TypeOf(new(PdfCpu))
|
||||
|
||||
if actual != expect {
|
||||
t.Errorf("expected '%s' but got '%s'", expect, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFcpu_Provision(t *testing.T) {
|
||||
mod := new(PDFcpu)
|
||||
func TestPdfCpu_Provision(t *testing.T) {
|
||||
engine := new(PdfCpu)
|
||||
ctx := gotenberg.NewContext(gotenberg.ParsedFlags{}, nil)
|
||||
|
||||
err := mod.Provision(ctx)
|
||||
err := engine.Provision(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error but got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFcpu_Merge(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
inputPaths []string
|
||||
expectErr bool
|
||||
func TestPdfCpu_Merge(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
scenario string
|
||||
inputPaths []string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
scenario: "invalid input path",
|
||||
inputPaths: []string{
|
||||
"foo",
|
||||
},
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
scenario: "single file success",
|
||||
inputPaths: []string{
|
||||
"/tests/test/testdata/pdfengines/sample1.pdf",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
scenario: "many files success",
|
||||
inputPaths: []string{
|
||||
"/tests/test/testdata/pdfengines/sample1.pdf",
|
||||
"/tests/test/testdata/pdfengines/sample2.pdf",
|
||||
},
|
||||
},
|
||||
{
|
||||
inputPaths: []string{
|
||||
"foo",
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
} {
|
||||
func() {
|
||||
mod := new(PDFcpu)
|
||||
|
||||
err := mod.Provision(nil)
|
||||
t.Run(tc.scenario, func(t *testing.T) {
|
||||
engine := new(PdfCpu)
|
||||
err := engine.Provision(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("test %d: expected error but got: %v", i, err)
|
||||
t.Fatalf("expected error but got: %v", err)
|
||||
}
|
||||
|
||||
fs := gotenberg.NewFileSystem()
|
||||
outputDir, err := fs.MkdirAll()
|
||||
if err != nil {
|
||||
t.Fatalf("test %d: expected error but got: %v", i, err)
|
||||
t.Fatalf("expected error but got: %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err := os.RemoveAll(fs.WorkingDirPath())
|
||||
err = os.RemoveAll(fs.WorkingDirPath())
|
||||
if err != nil {
|
||||
t.Fatalf("test %d: expected no error while cleaning up but got: %v", i, err)
|
||||
t.Fatalf("expected no error while cleaning up but got: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
err = mod.Merge(nil, nil, tc.inputPaths, outputDir+"/foo.pdf")
|
||||
err = engine.Merge(nil, nil, tc.inputPaths, outputDir+"/foo.pdf")
|
||||
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("test %d: expected error but got: %v", i, err)
|
||||
if !tc.expectError && err != nil {
|
||||
t.Fatalf("expected no error but got: %v", err)
|
||||
}
|
||||
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("test %d: expected no error but got: %v", i, err)
|
||||
if tc.expectError && err == nil {
|
||||
t.Fatal("expected error but got none")
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFcpu_Convert(t *testing.T) {
|
||||
mod := new(PDFcpu)
|
||||
err := mod.Convert(context.TODO(), zap.NewNop(), "", "", "")
|
||||
func TestPdfCpu_Convert(t *testing.T) {
|
||||
mod := new(PdfCpu)
|
||||
err := mod.Convert(context.TODO(), zap.NewNop(), gotenberg.PdfFormats{}, "", "")
|
||||
|
||||
if !errors.Is(err, gotenberg.ErrPDFEngineMethodNotAvailable) {
|
||||
t.Errorf("expected error %v, but got: %v", gotenberg.ErrPDFEngineMethodNotAvailable, err)
|
||||
if !errors.Is(err, gotenberg.ErrPdfEngineMethodNotSupported) {
|
||||
t.Errorf("expected error %v, but got: %v", gotenberg.ErrPdfEngineMethodNotSupported, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user