PDF is generating successfully but I want to protect it with a password. flying-saucer-pdf doc does not help me. I am using this example Using thymeleaf+flying-saucer-pdf+Spring Boot
Asked
Active
Viewed 1,760 times
6
-
First of all thanks for referencing PDF creation from my blog. To set password in PDF you will need to use `PDFEncryption` class of flying saucer – Ajit Soman Sep 03 '17 at 14:21
-
Protecting a pdf using password has nothing to do with spring or thymeleaf. Removing those tags. – Himanshu Bhardwaj Sep 03 '17 at 16:59
1 Answers
13
To set password in PDF with Flying Saucer
PDF Creator use PDFEncryption
class. To set password to your PDF, First create an instance of PDFEncryption
and then use its method setUserPassword()
like this:
final File outputFile = File.createTempFile(fileName, ".pdf");
FileOutputStream os = new FileOutputStream(outputFile);
PDFEncryption pdfEncryption = new PDFEncryption();
String password= "password@123";
pdfEncryption.setUserPassword(password.getBytes());
ITextRenderer renderer = new ITextRenderer();
renderer.setPDFEncryption(pdfEncryption);
renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(os, false);
renderer.finishPDF();

Ajit Soman
- 3,926
- 3
- 22
- 41