Write the source code analysis for the csdn blog extraction device by yourself. Part 3: Process webpages and save them as PDF files.

Source: Internet
Author: User

Next I will talk about how to process PDF files. Here I use PD4ML for processing. There are several reasons: (1) it supports CSS very well; (2), Can process pictures (very refreshing) (3), can process Chinese, but a little trouble. For the basic process, see my other post. java programming converts HTML files into PDF files: http://blog.csdn.net/w397090770/article/details/7754256. The following is my processing process:

/*****/Package com.wyp.html 2pdf; import Java. io. file; import Java. io. fileoutputstream; import Java. io. stringreader; import Org. zefer. pd4ml. pd4constants; import Org. zefer. pd4ml. pd4ml; import Org. zefer. pd4ml. pd4pagemark;/*** @ author w397090770 * create data: 2012-7-18 * Email: wyphao.2007@163.com ** All Rights Reserved. Skip this article, but add these notes when modifying this program. Thank you * for learning and communicating only. ** Save the webpage as PDF */public class saveaspdf {// the HTML code is from the HTML file public void generate0000_2 (File outputpdffile, string contents, String title) throws exception {file savefilename = new file (outputpdffile. getabsolutefile () + file. separator + title + ". PDF "); If (! Savefilename. exists () {savefilename. createnewfile () ;}else {return;} fileoutputstream Fos = new fileoutputstream (savefilename); pd4ml pd4ml = new pd4ml (); // header export headermark = new pd4pagemark (); headermark. setareaheight (30); headermark. setinitialpagenumber (1); headermark. setpagestoskip (1); headermark. settitlealignment (pd4pagemark. center_align); headermark. sethtmltemplate (title); // autocomputepd4ml. setpageheader (headermark); // footer pd4pagemark footermark = new pd4pagemark (); footermark. setareaheight (30); footermark. setinitialpagenumber (10); footermark. sethtmltemplate ("Wuhan University"); pd4ml. setpagefooter (footermark); // select the paper size, font directory, Font, and other pd4ml. setpagesize (pd4constants. a4); pd4ml. usettf ("file: fonts", true); pd4ml. setdefaultttfs ("youyuan", "Arial", "Courier New"); pd4ml. enabledebuginfo (); pd4ml. render (New stringreader (contents), FOS );}}

The code page is very simple.

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.