Search in sources :

Example 26 with PDDocument

use of org.apache.pdfbox.pdmodel.PDDocument in project camel by apache.

the class PdfCreationTest method testPdfCreationWithEncryption.

@Test
public void testPdfCreationWithEncryption() throws Exception {
    final String ownerPass = "ownerPass";
    final String userPass = "userPass";
    final String expectedText = "expectedText";
    AccessPermission accessPermission = new AccessPermission();
    accessPermission.setCanPrint(false);
    StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission);
    protectionPolicy.setEncryptionKeyLength(128);
    template.sendBodyAndHeader("direct:start", expectedText, PdfHeaderConstants.PROTECTION_POLICY_HEADER_NAME, protectionPolicy);
    resultEndpoint.setExpectedMessageCount(1);
    resultEndpoint.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            Object body = exchange.getIn().getBody();
            assertThat(body, instanceOf(ByteArrayOutputStream.class));
            try {
                PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
                assertTrue("Expected encrypted document", doc.isEncrypted());
                doc.decrypt(userPass);
                assertFalse("Printing should not be permitted", doc.getCurrentAccessPermission().canPrint());
                PDFTextStripper pdfTextStripper = new PDFTextStripper();
                String text = pdfTextStripper.getText(doc);
                assertEquals(1, doc.getNumberOfPages());
                assertThat(text, containsString(expectedText));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return true;
        }
    });
    resultEndpoint.assertIsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) StandardProtectionPolicy(org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) AccessPermission(org.apache.pdfbox.pdmodel.encryption.AccessPermission) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) Predicate(org.apache.camel.Predicate) PDFTextStripper(org.apache.pdfbox.util.PDFTextStripper) Test(org.junit.Test)

Example 27 with PDDocument

use of org.apache.pdfbox.pdmodel.PDDocument in project camel by apache.

the class PdfCreationTest method testPdfCreation.

@Test
public void testPdfCreation() throws Exception {
    final String expectedText = "expectedText";
    template.sendBody("direct:start", expectedText);
    resultEndpoint.setExpectedMessageCount(1);
    resultEndpoint.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            Object body = exchange.getIn().getBody();
            assertThat(body, instanceOf(ByteArrayOutputStream.class));
            try {
                PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
                PDFTextStripper pdfTextStripper = new PDFTextStripper();
                String text = pdfTextStripper.getText(doc);
                assertEquals(1, doc.getNumberOfPages());
                assertThat(text, containsString(expectedText));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return true;
        }
    });
    resultEndpoint.assertIsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) Predicate(org.apache.camel.Predicate) PDFTextStripper(org.apache.pdfbox.util.PDFTextStripper) Test(org.junit.Test)

Example 28 with PDDocument

use of org.apache.pdfbox.pdmodel.PDDocument in project camel by apache.

the class PdfProducer method doAppend.

private Object doAppend(Exchange exchange) throws IOException, BadSecurityHandlerException, CryptographyException, InvalidPasswordException, COSVisitorException {
    LOG.debug("Got {} operation, going to append text to provided pdf.", pdfConfiguration.getOperation());
    String body = exchange.getIn().getBody(String.class);
    PDDocument document = exchange.getIn().getHeader(PDF_DOCUMENT_HEADER_NAME, PDDocument.class);
    if (document == null) {
        throw new IllegalArgumentException(String.format("%s header is expected for append operation", PDF_DOCUMENT_HEADER_NAME));
    }
    if (document.isEncrypted()) {
        DecryptionMaterial decryptionMaterial = exchange.getIn().getHeader(DECRYPTION_MATERIAL_HEADER_NAME, DecryptionMaterial.class);
        if (decryptionMaterial == null) {
            throw new IllegalArgumentException(String.format("%s header is expected for %s operation " + "on encrypted document", DECRYPTION_MATERIAL_HEADER_NAME, pdfConfiguration.getOperation()));
        }
        document.openProtection(decryptionMaterial);
        document.setAllSecurityToBeRemoved(true);
    }
    ProtectionPolicy protectionPolicy = exchange.getIn().getHeader(PROTECTION_POLICY_HEADER_NAME, ProtectionPolicy.class);
    appendToPdfDocument(body, document, protectionPolicy);
    OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    document.save(byteArrayOutputStream);
    return byteArrayOutputStream;
}
Also used : DecryptionMaterial(org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StandardProtectionPolicy(org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy) ProtectionPolicy(org.apache.pdfbox.pdmodel.encryption.ProtectionPolicy)

Example 29 with PDDocument

use of org.apache.pdfbox.pdmodel.PDDocument in project camel by apache.

the class PdfProducer method doCreate.

private OutputStream doCreate(Exchange exchange) throws IOException, BadSecurityHandlerException, COSVisitorException {
    LOG.debug("Got {} operation, going to create and write provided string to pdf document.", pdfConfiguration.getOperation());
    String body = exchange.getIn().getBody(String.class);
    PDDocument document = new PDDocument();
    StandardProtectionPolicy protectionPolicy = exchange.getIn().getHeader(PROTECTION_POLICY_HEADER_NAME, StandardProtectionPolicy.class);
    appendToPdfDocument(body, document, protectionPolicy);
    OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    document.save(byteArrayOutputStream);
    return byteArrayOutputStream;
}
Also used : StandardProtectionPolicy(org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 30 with PDDocument

use of org.apache.pdfbox.pdmodel.PDDocument in project camel by apache.

the class FopComponentTest method createPdfUsingXmlDataAndXsltTransformation.

@Test
public void createPdfUsingXmlDataAndXsltTransformation() throws Exception {
    resultEndpoint.expectedMessageCount(1);
    FileInputStream inputStream = new FileInputStream("src/test/data/xml/data.xml");
    template.sendBody(inputStream);
    resultEndpoint.assertIsSatisfied();
    PDDocument document = PDDocument.load("target/data/result.pdf");
    String pdfText = FopHelper.extractTextFrom(document);
    //from xsl template
    assertTrue(pdfText.contains("Project"));
    //from data xml
    assertTrue(pdfText.contains("John Doe"));
    // assert on the header "foo" being populated
    Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
    assertEquals("Header value is lost!", "bar", exchange.getIn().getHeader("foo"));
}
Also used : Exchange(org.apache.camel.Exchange) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

PDDocument (org.apache.pdfbox.pdmodel.PDDocument)52 Test (org.junit.Test)23 IOException (java.io.IOException)15 PDPage (org.apache.pdfbox.pdmodel.PDPage)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Exchange (org.apache.camel.Exchange)10 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)9 XMPMetadata (org.apache.jempbox.xmp.XMPMetadata)8 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)8 BibEntry (org.jabref.model.entry.BibEntry)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 LinkedList (java.util.LinkedList)6 Predicate (org.apache.camel.Predicate)6 XMPSchema (org.apache.jempbox.xmp.XMPSchema)6 XMPSchemaDublinCore (org.apache.jempbox.xmp.XMPSchemaDublinCore)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 StandardProtectionPolicy (org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy)5 PDFTextStripper (org.apache.pdfbox.util.PDFTextStripper)5 File (java.io.File)4 PDPageContentStream (org.apache.pdfbox.pdmodel.edit.PDPageContentStream)4