mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 08:32:16 +01:00
125 lines
3.6 KiB
Go
125 lines
3.6 KiB
Go
package qpdf
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/gotenberg/gotenberg/v8/pkg/gotenberg"
|
|
)
|
|
|
|
func init() {
|
|
gotenberg.MustRegisterModule(new(QPdf))
|
|
}
|
|
|
|
// QPdf abstracts the CLI tool QPDF and implements the [gotenberg.PdfEngine]
|
|
// interface.
|
|
type QPdf struct {
|
|
binPath string
|
|
}
|
|
|
|
// Descriptor returns a [QPdf]'s module descriptor.
|
|
func (engine *QPdf) Descriptor() gotenberg.ModuleDescriptor {
|
|
return gotenberg.ModuleDescriptor{
|
|
ID: "qpdf",
|
|
New: func() gotenberg.Module { return new(QPdf) },
|
|
}
|
|
}
|
|
|
|
// Provision sets the modules properties.
|
|
func (engine *QPdf) Provision(ctx *gotenberg.Context) error {
|
|
binPath, ok := os.LookupEnv("QPDF_BIN_PATH")
|
|
if !ok {
|
|
return errors.New("QPDF_BIN_PATH environment variable is not set")
|
|
}
|
|
|
|
engine.binPath = binPath
|
|
|
|
return nil
|
|
}
|
|
|
|
// Validate validates the module properties.
|
|
func (engine *QPdf) Validate() error {
|
|
_, err := os.Stat(engine.binPath)
|
|
if os.IsNotExist(err) {
|
|
return fmt.Errorf("QPDF binary path does not exist: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Split splits a given PDF file.
|
|
func (engine *QPdf) Split(ctx context.Context, logger *zap.Logger, mode gotenberg.SplitMode, inputPath, outputDirPath string) ([]string, error) {
|
|
var args []string
|
|
outputPath := fmt.Sprintf("%s/%s", outputDirPath, filepath.Base(inputPath))
|
|
|
|
switch mode.Mode {
|
|
case gotenberg.SplitModePages:
|
|
if !mode.Unify {
|
|
return nil, fmt.Errorf("split PDFs using mode '%s' without unify with QPDF: %w", mode.Mode, gotenberg.ErrPdfSplitModeNotSupported)
|
|
}
|
|
args = append(args, inputPath, "--pages", ".", mode.Span, "--", outputPath)
|
|
default:
|
|
return nil, fmt.Errorf("split PDFs using mode '%s' with QPDF: %w", mode.Mode, gotenberg.ErrPdfSplitModeNotSupported)
|
|
}
|
|
|
|
cmd, err := gotenberg.CommandContext(ctx, logger, engine.binPath, args...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create command: %w", err)
|
|
}
|
|
|
|
_, err = cmd.Exec()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("split PDFs with QPDF: %w", err)
|
|
}
|
|
|
|
return []string{outputPath}, nil
|
|
}
|
|
|
|
// Merge combines multiple PDFs into a single PDF.
|
|
func (engine *QPdf) Merge(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
|
var args []string
|
|
args = append(args, "--empty")
|
|
args = append(args, "--pages")
|
|
args = append(args, inputPaths...)
|
|
args = append(args, "--", outputPath)
|
|
|
|
cmd, err := gotenberg.CommandContext(ctx, logger, engine.binPath, args...)
|
|
if err != nil {
|
|
return fmt.Errorf("create command: %w", err)
|
|
}
|
|
|
|
_, err = cmd.Exec()
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("merge PDFs with QPDF: %w", err)
|
|
}
|
|
|
|
// Convert is not available in this implementation.
|
|
func (engine *QPdf) Convert(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error {
|
|
return fmt.Errorf("convert PDF to '%+v' with QPDF: %w", formats, gotenberg.ErrPdfEngineMethodNotSupported)
|
|
}
|
|
|
|
// ReadMetadata is not available in this implementation.
|
|
func (engine *QPdf) ReadMetadata(ctx context.Context, logger *zap.Logger, inputPath string) (map[string]interface{}, error) {
|
|
return nil, fmt.Errorf("read PDF metadata with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported)
|
|
}
|
|
|
|
// WriteMetadata is not available in this implementation.
|
|
func (engine *QPdf) WriteMetadata(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error {
|
|
return fmt.Errorf("write PDF metadata with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported)
|
|
}
|
|
|
|
var (
|
|
_ gotenberg.Module = (*QPdf)(nil)
|
|
_ gotenberg.Provisioner = (*QPdf)(nil)
|
|
_ gotenberg.Validator = (*QPdf)(nil)
|
|
_ gotenberg.PdfEngine = (*QPdf)(nil)
|
|
)
|