mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 00:22:14 +01:00
feat: add PDF encryption feature (#1217)
* Adding PDF encryption option * LibreOffice * QPDF * PDFtk * pdfcpu * Update pkg/modules/pdfengines/pdfengines.go Co-authored-by: Julien Neuhart <neuhart.julien@gmail.com> * npx prettier * go fmt * PR comments * Update test/integration/scenario/scenario.go Co-authored-by: Julien Neuhart <neuhart.julien@gmail.com> * renamed ProtectWithPassword to encrypt * more clean up * Use the same input path as requested * This commit completes the encryption implementation * Clean up. Added webhook, disable route and download from tests --------- Co-authored-by: Julien Neuhart <neuhart.julien@gmail.com>
This commit is contained in:
committed by
Julien Neuhart
parent
638c4e6b23
commit
99307befdd
@@ -145,6 +145,43 @@ func (engine *PdfTk) WriteMetadata(ctx context.Context, logger *zap.Logger, meta
|
||||
return fmt.Errorf("write PDF metadata with PDFtk: %w", gotenberg.ErrPdfEngineMethodNotSupported)
|
||||
}
|
||||
|
||||
// Encrypt adds password protection to a PDF file using PDFtk.
|
||||
func (engine *PdfTk) Encrypt(ctx context.Context, logger *zap.Logger, inputPath, userPassword, ownerPassword string) error {
|
||||
if userPassword == "" {
|
||||
return errors.New("user password cannot be empty")
|
||||
}
|
||||
|
||||
// If owner password is not provided, use the user password as owner password
|
||||
if ownerPassword == "" {
|
||||
ownerPassword = userPassword
|
||||
}
|
||||
|
||||
var args []string
|
||||
args = append(args, inputPath)
|
||||
args = append(args, "output", inputPath)
|
||||
args = append(args, "encrypt_128bit")
|
||||
|
||||
if userPassword != "" {
|
||||
args = append(args, "user_pw", userPassword)
|
||||
}
|
||||
|
||||
if ownerPassword != "" {
|
||||
args = append(args, "owner_pw", ownerPassword)
|
||||
}
|
||||
|
||||
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 PDFtk: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Interface guards.
|
||||
var (
|
||||
_ gotenberg.Module = (*PdfTk)(nil)
|
||||
|
||||
Reference in New Issue
Block a user