Showing posts with label iText. Show all posts
Showing posts with label iText. Show all posts

Sunday, May 25, 2014

Adding Footer and Header into PDF with iText

iText version - 2.1.7

Header/Footer only
Document document = new Document();
HeaderFooter header = new HeaderFooter(new Phrase("This is header"), false);
HeaderFooter footer = new HeaderFooter(new Phrase("This is footer"), false);
document.setHeader(header);
document.setFooter(footer);


Header/Footer with page number
Document document = new Document();
HeaderFooter header = new HeaderFooter(new Phrase("This is header"), true);
HeaderFooter footer = new HeaderFooter(new Phrase("This is footer"), true);
document.setHeader(header);
document.setFooter(footer);


Header/Footer with font
Document document = new Document();
Font font = new Font(Font.TIMES_ROMAN, 11);  // font family, font size
HeaderFooter header = new HeaderFooter(new Phrase("This is header", font), true);
HeaderFooter footer =new HeaderFooter(new Phrase("This is footer", font), false);
document.setHeader(header);
document.setFooter(footer);


Done!!

Wednesday, March 26, 2014

How to generate styled PDF in Liferay PrimeFaces Portlet with iText

In the previous post,
How to generate PDF in Liferay PrimeFaces Portlet with iText shows how to generate a PDF document, unfortunately, the generated PDF is not able to handle style class.

eg,
<!DOCTYPE HTML><html><body>Generate <strong>PDF</strong> Test</body></html>
can be generated without any problem. 

but, 
<!DOCTYPE HTML><html><body>Generate <span style='color: red;'>PDF</span> Test</body></html>
is unable to generate the text with red color.

Thursday, January 16, 2014

How to generate PDF in Liferay PrimeFaces Portlet with iText

The objective of this post is to show how to generate PDF with iText in Liferay PrimeFaces Portlet.
The idea is to generate the PDF stream with iText and then download it with <p:fileDownload />.

Platform:
Liferay 6.1.1
PrimeFaces 3.5
iText 2.0.8 and core-renderer-R8

Steps:
1. Create the content in PDF stream with iText
    public byte[] getPdfStream() {
      byte[] pdf = null;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      String html = "<!DOCTYPE HTML><html><body>Generate PDF Test</body></html>";
      try {
              ITextRenderer itextRenderer = new ITextRenderer();
              itextRenderer.setDocumentFromString(html);
              itextRenderer.layout();
              itextRenderer.createPDF(baos);
              pdf = baos.toByteArray();
      }
      catch (Exception e) {
       e.printStackTrace();
      }
    }


2. Prepare the StreamedContent as <p:fileDownload /> value
    public StreamedContent getPdfFile() throws Exception {
     PortletResponse portletResponse = (PortletResponse) FacesContext
      .getCurrentInstance().getExternalContext().getResponse();
     HttpServletResponse res = PortalUtil
      .getHttpServletResponse(portletResponse);
     res.setHeader("Content-Disposition", "attachment; filename=\""
          + "test-kian.pdf" + "\"");//
     res.setHeader("Content-Transfer-Encoding", "binary");
     res.setContentType("application/pdf");

  res.flushBuffer();
  OutputStream out = res.getOutputStream();
  out.write(getPdfStream());

     return null;
    }


3. Download the PDF with <p:fileDownload /> in xhtml
<p:commandButton value="download PDF" ajax="false">
    <p:fileDownload value="#{myBean.pdfFile}" />
</p:commandButton>

4. To generate styled PDF, please refer How to generate styled PDF in Liferay PrimeFaces Portlet with iText


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...