fix(pdgengines): encrypt behavior accross different engines

This commit is contained in:
Julien Neuhart
2025-09-01 20:26:34 +02:00
parent ba944f9dc2
commit 0ad6623e24
8 changed files with 165 additions and 19 deletions

View File

@@ -270,6 +270,16 @@ func EncryptPdfStub(ctx *api.Context, engine gotenberg.PdfEngine, userPassword,
for _, inputPath := range inputPaths {
err := engine.Encrypt(ctx, ctx.Log(), inputPath, userPassword, ownerPassword)
if err != nil {
if errors.Is(err, gotenberg.ErrPdfEngineEncryptionPasswordsNotSupported) {
return api.WrapError(
err,
api.NewSentinelHttpError(
http.StatusBadRequest,
"Invalid form data: both 'userPassword' and 'ownerPassword' form fields must be provided and different",
),
)
}
return fmt.Errorf("encrypt PDF '%s': %w", inputPath, err)
}
}

View File

@@ -151,13 +151,16 @@ func (engine *PdfTk) Encrypt(ctx context.Context, logger *zap.Logger, inputPath,
return errors.New("user password cannot be empty")
}
if ownerPassword == "" {
ownerPassword = userPassword
if ownerPassword == userPassword || ownerPassword == "" {
return gotenberg.ErrPdfEngineEncryptionPasswordsNotSupported
}
// Create a temp output file in the same directory.
tmpPath := inputPath + ".tmp"
var args []string
args = append(args, inputPath)
args = append(args, "output", inputPath)
args = append(args, "output", tmpPath)
args = append(args, "encrypt_128bit")
args = append(args, "user_pw", userPassword)
args = append(args, "owner_pw", ownerPassword)
@@ -172,6 +175,11 @@ func (engine *PdfTk) Encrypt(ctx context.Context, logger *zap.Logger, inputPath,
return fmt.Errorf("encrypt PDF with PDFtk: %w", err)
}
err = os.Rename(tmpPath, inputPath)
if err != nil {
return fmt.Errorf("rename temporary output file with input file: %w", err)
}
return nil
}

View File

@@ -185,8 +185,8 @@ func (engine *QPdf) Encrypt(ctx context.Context, logger *zap.Logger, inputPath,
var args []string
args = append(args, inputPath)
args = append(args, engine.globalArgs...)
args = append(args, "--encrypt", userPassword, ownerPassword, "256", "--use-aes=y", "--")
args = append(args, inputPath)
args = append(args, "--replace-input")
args = append(args, "--encrypt", userPassword, ownerPassword, "256", "--")
cmd, err := gotenberg.CommandContext(ctx, logger, engine.binPath, args...)
if err != nil {