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

大憨熊 提交于 2019-12-08 20:31:53

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/46024239/how-to-protect-pdf-report-with-password-using-thymeleaf-as-template-engine-and-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!