Use flying saucer and itext to convert HTML to PDF (Java)

Source: Internet
Author: User
Document directory
  • Flying saucer and itext:
  • Generate a simple PDF
  • Use fly to generate content
  • Create a PDF file on the server

PDF is one of the most popular document formats so far. Generally, to create a PDF file, you need to use Adobe's API to generate it, but this is very troublesome. In order to easily generate PDF documents using HTML (XHTML), the following describes two open-source Java libraries: flying saucer and itext.

Flying saucer and itext:

Flying saucer (or xhtmlrender project on java.net) is an itext-based open-source Java library that can easily generate PDF files from HTML (with css2.1.

Itext is an open-source Java library for generating PDF documents. It can dynamically generate PDF files from XML or databases, and has the vast majority of attributes of PDF documents (such as encryption ......), Supports Java, C #, and so on.

Generate a simple PDF

Next we will first create a simple HTML with CSS. The Code is as follows:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Next, generate a PDF file.
package oliver.itext.html2pdf;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import org.xhtmlrenderer.pdf.ITextRenderer;import com.lowagie.text.DocumentException;public class FirstDoc{    public static void main(String[] args) throws DocumentException, IOException    {        String path = System.getProperty("user.dir") + "/src/";        String inputFile = path + "samples/firstdoc.html";        String url = new File(inputFile).toURI().toURL().toString();        String outputFile = path + "outputs/fistdoc.pdf";        OutputStream os = new FileOutputStream(outputFile);        ITextRenderer render = new ITextRenderer();        render.setDocument(url);        render.layout();        render.createPDF(os);        os.close();    }}
The result is as follows: Use fly to generate content.

In the following example, we first generate an HTML string using stringbuilder, and then parse it using Dom to generate a PDF.

package com.sankuai.meituan.ct.doamin;import java.io.FileOutputStream;import java.io.OutputStream;import java.io.StringBufferInputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import  org.xhtmlrenderer.pdf.ITextRenderer;@SuppressWarnings("deprecation")public class OneHundredBottles{    public static void main(String[] args) throws Exception    {        String path =System.getProperty("user.dir")+"/src/";        StringBuffer buf = new StringBuffer();        buf.append("
The effect is as follows:

Create a PDF file on the server

The following figure shows how to create a PDF file in the servlet.

public class PDFServlet extends HttpServlet {        protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        response.setContentType("application/pdf");        StringBuffer buf = newStringBuffer();        buf.append("

Address: http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.