Skip to content

Instantly share code, notes, and snippets.

@indaco
Created May 14, 2010 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indaco/401005 to your computer and use it in GitHub Desktop.
Save indaco/401005 to your computer and use it in GitHub Desktop.
Generate PDF from XDP
...
ByteArrayInputStream pdfInputStream = null;
try {
IWDPDFDocumentHandler pdfDocumentHandler = WDPDFDocumentFactory.getDocumentHandler();
IWDPDFDocumentCreationContext pdfDocCreationContext = pdfDocumentHandler.getDocumentCreationContext();
String templateUrl = WDURLGenerator.getResourcePath(wdComponentAPI.getDeployableObjectPart(),this.xdpSource);
InputStream template = new FileInputStream(templateUrl);
ByteArrayOutputStream templateStream = inputToOutputStream(template);
byte[] pdfStream = wdContext.currentContextElement().getPdfSource();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(pdfStream);
} catch (IOException e) {
this.msgMgr.reportException(e.getMessage());
}
pdfDocCreationContext.setData(baos);
pdfDocCreationContext.setTemplate(templateStream);
pdfDocCreationContext.setDynamic(true);
IWDPDFDocument pdf = pdfDocCreationContext.execute();
pdfInputStream = (ByteArrayInputStream) pdf.getData();//PDFAsStream();
try {
this.msgMgr.reportSuccess(convertStreamToString(pdfInputStream));
} catch (IOException e) {
this.msgMgr.reportException(e.getMessage());
}
IWDResource pdfResource =
WDResourceFactory.createCachedResource(pdf.getPDF(), "test.pdf", WDWebResourceType.PDF);
//pdfResource element is bounded to FileDownload UI element resource property
wdContext.currentContextElement().setPdfResource(pdfResource);
} catch (Exception e) {
this.msgMgr.reportException(e.getMessage());
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment