How to protect a pdf report with a password using thimeleaf as a template engine and a flying saucer as a rendrer

PDF is being generated successfully, but I want to protect it with a password. I can not help. I am using this example Using thymeleaf + flying-saucer-pdf + Spring Download

+5
source share
2 answers

To set a password in PDF using Flying Saucer, the PDF creator uses a class PDFEncryption. To set a password for your PDF file, first create an instance PDFEncryptionand then use its method setUserPassword()as follows:

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();
+9
source

XHTML. PDF , . , , .

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();

// try to uncomment the following line
// renderer.setPDFEncryption(pdfEncryption);

renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(os, false);
renderer.finishPDF();

?

0

Source: https://habr.com/ru/post/1684980/


All Articles