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();
}
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();
}
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;
}
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;
}
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"));
}
Aggregations