Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to protect pdf report with password using thymeleaf as template engine and flying-saucer as rendrer

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

like image 262
AsinghrawatZ Avatar asked Sep 03 '17 14:09

AsinghrawatZ


1 Answers

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();
like image 181
Ajit Soman Avatar answered Sep 20 '22 14:09

Ajit Soman