public static void exportToPdf(final OutputStream os, final String html, final String url, final PdfRendererBuilder.TextDirection defaultTextDirection, final ProtectionPolicy protectionPolicy) { PdfBoxRenderer renderer = null; try { // There are more options on the builder than shown below. PdfRendererBuilder builder = new PdfRendererBuilder(); handleTextDirection(defaultTextDirection, builder); handleW3cDocument(html, url, builder); builder.toStream(os); renderer = builder.buildPdfRenderer(); PDDocument document = renderer.getPdfDocument(); if (protectionPolicy != null) document.protect(protectionPolicy); renderer.layout(); renderer.createPDF(); } catch (Exception e) { e.printStackTrace(); // LOG exception } finally { try { if (renderer != null) { renderer.close(); } os.close(); } catch (IOException e) { // swallow } } }
private void appendToPdfDocument(String text, PDDocument document, ProtectionPolicy protectionPolicy) throws IOException { Collection<String> words = splitStrategy.split(text); Collection<String> lines = lineBuilderStrategy.buildLines(words); writeStrategy.write(lines, document); if (protectionPolicy != null) { document.protect(protectionPolicy); } }
PipedOutputStream os = new PipedOutputStream(); PipedInputStream is = new PipedInputStream(os); System.setProperty("org.apache.pdfbox.baseParser.pushBackSize", "2024768"); InputStream dataStream = secureData.data(); PDDocument doc = PDDocument.load(dataStream, true); AccessPermission ap = new AccessPermission(); //add what ever perms you need blah blah... ap.setCanModify(false); ap.setCanExtractContent(false); ap.setCanPrint(false); ap.setCanPrintDegraded(false); ap.setReadOnly(); StandardProtectionPolicy spp = new StandardProtectionPolicy(UUID.randomUUID().toString(), "", ap); doc.protect(spp); doc.save(os); doc.close(); dataStream.close(); os.close();
private PDDocument getDocument(RandomAccessRead source) throws IOException, BleachException { PDDocument doc; for (String pwd : COMMON_PASSWORDS) { ScratchFile scratchFile = new ScratchFile(MEMORY_USAGE_SETTING); doc = testPassword(scratchFile, source, pwd); if (doc != null) { LOGGER.debug("Password was guessed: '{}'", pwd); doc.protect(new StandardProtectionPolicy(pwd, pwd, doc.getCurrentAccessPermission())); return doc; } scratchFile.close(); } // @TODO: fetch password from config? throw new BleachException("PDF is protected with an unknown password"); }
public static void exportToPdf(final OutputStream os, final String html, final String url, final PdfRendererBuilder.TextDirection defaultTextDirection, final ProtectionPolicy protectionPolicy) { PdfBoxRenderer renderer = null; try { // There are more options on the builder than shown below. PdfRendererBuilder builder = new PdfRendererBuilder(); handleTextDirection(defaultTextDirection, builder); handleW3cDocument(html, url, builder); builder.toStream(os); renderer = builder.buildPdfRenderer(); PDDocument document = renderer.getPdfDocument(); if (protectionPolicy != null) document.protect(protectionPolicy); renderer.layout(); renderer.createPDF(); } catch (Exception e) { e.printStackTrace(); // LOG exception } finally { try { if (renderer != null) { renderer.close(); } os.close(); } catch (IOException e) { // swallow } } }
spp.setEncryptionKeyLength(keyLength); pdfDoc = PDDocument.load(pdfBlob.getFile()); pdfDoc.protect(spp); Blob result = Blobs.createBlobWithExtension(".pdf"); pdfDoc.save(result.getFile());