mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 00:22:14 +01:00
feat: add 7.x source code
This commit is contained in:
4
pkg/modules/pdfcpu/doc.go
Normal file
4
pkg/modules/pdfcpu/doc.go
Normal file
@@ -0,0 +1,4 @@
|
||||
// Package pdfcpu provides a module which wraps the
|
||||
// https://github.com/pdfcpu/pdfcpu library and implements the
|
||||
// gotenberg.PDFEngine interface.
|
||||
package pdfcpu
|
||||
62
pkg/modules/pdfcpu/pdfcpu.go
Normal file
62
pkg/modules/pdfcpu/pdfcpu.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package pdfcpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
||||
pdfcpuAPI "github.com/pdfcpu/pdfcpu/pkg/api"
|
||||
pdfcpuLog "github.com/pdfcpu/pdfcpu/pkg/log"
|
||||
pdfcpuConfig "github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gotenberg.MustRegisterModule(PDFcpu{})
|
||||
}
|
||||
|
||||
// PDFcpu is a module which wraps the https://github.com/pdfcpu/pdfcpu library
|
||||
// and implements the gotenberg.PDFEngine interface.
|
||||
type PDFcpu struct {
|
||||
conf *pdfcpuConfig.Configuration
|
||||
}
|
||||
|
||||
// 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) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets the engine properties.
|
||||
func (engine *PDFcpu) Provision(_ *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 {
|
||||
err := pdfcpuAPI.MergeCreateFile(inputPaths, outputPath, engine.conf)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Interface guards.
|
||||
var (
|
||||
_ gotenberg.Module = (*PDFcpu)(nil)
|
||||
_ gotenberg.Provisioner = (*PDFcpu)(nil)
|
||||
_ gotenberg.PDFEngine = (*PDFcpu)(nil)
|
||||
)
|
||||
98
pkg/modules/pdfcpu/pdfcpu_test.go
Normal file
98
pkg/modules/pdfcpu/pdfcpu_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package pdfcpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestPDFcpu_Descriptor(t *testing.T) {
|
||||
descriptor := PDFcpu{}.Descriptor()
|
||||
|
||||
actual := reflect.TypeOf(descriptor.New())
|
||||
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)
|
||||
ctx := gotenberg.NewContext(gotenberg.ParsedFlags{}, nil)
|
||||
|
||||
err := mod.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
|
||||
}{
|
||||
{
|
||||
inputPaths: []string{
|
||||
"/tests/test/testdata/pdfengines/sample1.pdf",
|
||||
},
|
||||
},
|
||||
{
|
||||
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)
|
||||
if err != nil {
|
||||
t.Fatalf("test %d: expected error but got: %v", i, err)
|
||||
}
|
||||
|
||||
outputDir, err := gotenberg.MkdirAll()
|
||||
if err != nil {
|
||||
t.Fatalf("test %d: expected error but got: %v", i, err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err := os.RemoveAll(outputDir)
|
||||
if err != nil {
|
||||
t.Fatalf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
}()
|
||||
|
||||
err = mod.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.expectErr && err != nil {
|
||||
t.Errorf("test %d: expected no error but got: %v", i, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFcpu_Convert(t *testing.T) {
|
||||
mod := new(PDFcpu)
|
||||
err := mod.Convert(context.TODO(), zap.NewNop(), "", "", "")
|
||||
|
||||
if !errors.Is(err, gotenberg.ErrPDFEngineMethodNotAvailable) {
|
||||
t.Errorf("expected error %v, but got: %v", gotenberg.ErrPDFEngineMethodNotAvailable, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user