Use Java to convert HTML to PDF

Source: Internet
Author: User

Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. outputstream;

Import org.xhtmlrenderer.pdf. itextfontresolver;
Import org.xhtmlrendererer. itextrenderer;

Import com.lowagie.text=. basefont;

Public class testflyingsauser
{

Public static void main (string [] ARGs) throws exception
{
// Demo_1 ();
Demo_2 ();
}

// Chinese characters are not supported
Public static void demo_1 () throws exception
{
String inputfile = "D:/test/flying.html ";
String url = new file (inputfile). touri (). tourl (). tostring ();
String outputfile = "D:/test/flyingdeletion ";
Outputstream OS = new fileoutputstream (outputfile );
Itextrenderer Renderer = new itextrenderer ();
Renderer. setdocument (URL );
Renderer. layout ();
Renderer. createpdf (OS );
OS. Close ();
}

// Supports Chinese Characters
Public static void demo_2 () throws exception {
String outputfile = "G:/demo_3.pdf ";
Outputstream OS = new fileoutputstream (outputfile );
Itextrenderer Renderer = new itextrenderer ();
Itextfontresolver fontresolver = Renderer. getfontresolver ();
Fontresolver. addfont ("C:/Windows/fonts/simsun. TTC", basefont. identity_h, basefont. not_embedded );

Stringbuffer html = new stringbuffer ();
// Doctype must be written. Otherwise, an error occurs when parsing a character similar to this one.
Html. append ("<! Doctype HTML public \ "-// W3C // dtd xhtml 1.0 transitional // en \" \ "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd\"> ");

Html. append ("<HTML xmlns = \" http://www.w3.org/5o/xhtml\ "> ").
Append (". Append ("<meta http-equiv = \" Content-Type \ "content = \" text/html; charset = UTF-8 \ "/> ")
. Append ("<style type = \" text/CSS \ "mce_bogus = \" 1 \ "> body {font-family: simsun ;}</style> ")

. Append (". Append ("<body> ");
Html. append ("<div> supports Chinese! </Div> ");
Html. append ("</body> Renderer. setdocumentfromstring (html. tostring ());
// Solve the relative path of the image
// Renderer. getsharedcontext (). setbaseurl ("file:/F:/teste/html /");
Renderer. layout ();
Renderer. createpdf (OS );
OS. Close ();
}}

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.