package qpdf import ( "bytes" "context" "errors" "fmt" "log/slog" "os" "os/exec" "path/filepath" "syscall" "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 globalArgs []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 module 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 // Warnings should not cause errors. engine.globalArgs = []string{"--warning-exit-0"} 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 } // Debug returns additional debug data. func (engine *QPdf) Debug() map[string]any { debug := make(map[string]any) cmd := exec.Command(engine.binPath, "--version") //nolint:gosec cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} output, err := cmd.Output() if err != nil { debug["version"] = err.Error() return debug } lines := bytes.SplitN(output, []byte("\n"), 2) if len(lines) > 0 { debug["version"] = string(lines[0]) } else { debug["version"] = "Unable to determine QPDF version" } return debug } // Split splits a given PDF file. func (engine *QPdf) Split(ctx context.Context, logger *slog.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) args = append(args, engine.globalArgs...) args = append(args, "--pages", ".", mode.Span) args = append(args, "--", 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 *slog.Logger, inputPaths []string, outputPath string) error { args := make([]string, 0, 4+len(engine.globalArgs)+len(inputPaths)) args = append(args, "--empty") args = append(args, engine.globalArgs...) 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) } // Flatten merges annotation appearances with page content, deleting the // original annotations. func (engine *QPdf) Flatten(ctx context.Context, logger *slog.Logger, inputPath string) error { args := make([]string, 0, 4+len(engine.globalArgs)) args = append(args, inputPath) args = append(args, "--generate-appearances") args = append(args, "--flatten-annotations=all") args = append(args, "--replace-input") args = append(args, engine.globalArgs...) 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("flatten PDFs with QPDF: %w", err) } // Convert is not available in this implementation. func (engine *QPdf) Convert(ctx context.Context, logger *slog.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 *slog.Logger, inputPath string) (map[string]any, 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 *slog.Logger, metadata map[string]any, inputPath string) error { return fmt.Errorf("write PDF metadata with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported) } // PageCount is not available in this implementation. func (engine *QPdf) PageCount(ctx context.Context, logger *slog.Logger, inputPath string) (int, error) { return 0, fmt.Errorf("page count with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported) } // WriteBookmarks is not available in this implementation. func (engine *QPdf) WriteBookmarks(ctx context.Context, logger *slog.Logger, inputPath string, bookmarks []gotenberg.Bookmark) error { return fmt.Errorf("write PDF bookmarks with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported) } // ReadBookmarks is not available in this implementation. func (engine *QPdf) ReadBookmarks(ctx context.Context, logger *slog.Logger, inputPath string) ([]gotenberg.Bookmark, error) { return nil, fmt.Errorf("read PDF bookmarks with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported) } // Encrypt adds password protection to a PDF file using QPDF. func (engine *QPdf) Encrypt(ctx context.Context, logger *slog.Logger, inputPath, userPassword, ownerPassword string) error { if userPassword == "" { return errors.New("user password cannot be empty") } if ownerPassword == "" { ownerPassword = userPassword } args := make([]string, 0, 7+len(engine.globalArgs)) args = append(args, inputPath) args = append(args, engine.globalArgs...) args = append(args, "--replace-input") args = append(args, "--encrypt", userPassword, ownerPassword, "256", "--") 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 fmt.Errorf("encrypt PDF with QPDF: %w", err) } return nil } // EmbedFiles is not available in this implementation. func (engine *QPdf) EmbedFiles(ctx context.Context, logger *slog.Logger, filePaths []string, inputPath string) error { return fmt.Errorf("embed files with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported) } // Watermark is not available in this implementation. func (engine *QPdf) Watermark(ctx context.Context, logger *slog.Logger, inputPath string, stamp gotenberg.Stamp) error { return fmt.Errorf("watermark PDF with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported) } // Stamp is not available in this implementation. func (engine *QPdf) Stamp(ctx context.Context, logger *slog.Logger, inputPath string, stamp gotenberg.Stamp) error { return fmt.Errorf("stamp PDF with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported) } // Rotate is not available in this implementation. func (engine *QPdf) Rotate(ctx context.Context, logger *slog.Logger, inputPath string, angle int, pages string) error { return fmt.Errorf("rotate PDF with QPDF: %w", gotenberg.ErrPdfEngineMethodNotSupported) } var ( _ gotenberg.Module = (*QPdf)(nil) _ gotenberg.Provisioner = (*QPdf)(nil) _ gotenberg.Validator = (*QPdf)(nil) _ gotenberg.Debuggable = (*QPdf)(nil) _ gotenberg.PdfEngine = (*QPdf)(nil) )