160603. Tool class using Pd4ml.jar and Ss_css2.jar to PDF

Source: Internet
Author: User
Tags readfile

Note: You need to import Pd4ml.jar and Ss_css2.jar

Import Java.awt.Insets;
Import Java.io.BufferedInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.StringReader;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.security.InvalidParameterException;
Import org.zefer.pd4ml.PD4Constants;
Import Org.zefer.pd4ml.PD4ML;
/***
* Pd4ml.jar,ss_css2.jar
* @author Admin
*/
public class Topdfutils {
protected int topvalue = 10;
protected int leftvalue = 20;
protected int rightvalue = 10;
protected int bottomvalue = 10;
protected int userspacewidth = 1300;
public static void Main (string[] args) throws Invalidparameterexception, Malformedurlexception, IOException {
Topdfutils pdfutils = new Topdfutils ();
Pdfutils.doconversion ("http://pd4ml.com/sample.htm", "g:/test/pd4ml.pdf");//Network address
String html = readFile ("g:/test/confirm.html", "UTF-8"); File address
Pdfutils.doconversion2 (HTML, "G:/test/pd4ml2.pdf");
}

/** convert the file to PDF, the source file is the network address starting with/HTTP//
public void doconversion (string url, string outputPath)
Throws Invalidparameterexception, Malformedurlexception,
IOException {
File output = new file (OutputPath);
FileOutputStream fos = new FileOutputStream (output);
pd4ml pd4ml = new pd4ml ();
Pd4ml.sethtmlwidth (Userspacewidth);
Pd4ml.setpagesize (Pd4ml.changepageorientation (pd4constants.a4));
PD4ML.SETPAGEINSETSMM (New Insets (Topvalue, Leftvalue, Bottomvalue,rightvalue));
Pd4ml.addstyle ("BODY {margin:0}", true);
Pd4ml.usettf ("Java:fonts", true);//src the font settings in the Fonts folder
Pd4ml.render (new URL (URL), FOS);
Fos.close ();
System.out.println (OutputPath + "\ndone.");
}

public void DoConversion2 (string htmldocument, String outputPath)
Throws Invalidparameterexception, Malformedurlexception, IOException {
pd4ml pd4ml = new pd4ml ();
Pd4ml.sethtmlwidth (Userspacewidth);
Select the format of the destination file
Pd4ml.setpagesize (Pd4ml.changepageorientation (pd4constants.a4));
Set margins
PD4ML.SETPAGEINSETSMM (New Insets (Topvalue, Leftvalue, Bottomvalue, Rightvalue));
The original HTML document also has margins, which can be compressed in this way
Pd4ml.addstyle ("BODY {margin:0}", true);
If the built-in basic PDF font is not enough, you can set it to Non-latin,ttf to do this
Pd4ml.usettf ("Java:fonts", true);
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Pd4ml.render (new StringReader (HTMLDocument), BAOs);
Baos.close ();
File output = new file (OutputPath);
FileOutputStream fos = new FileOutputStream (output);
Fos.write (Baos.tobytearray ());
Fos.close ();
System.out.println (OutputPath + "\ndone.");
}

Private final static string ReadFile (string path, string encoding) throws IOException {
File F = new file (path);
FileInputStream is = new FileInputStream (f);
Bufferedinputstream bis = new Bufferedinputstream (IS);
Bytearrayoutputstream fos = new Bytearrayoutputstream ();
byte buffer[] = new byte[2048];
int read;
do {
Read = is.read (buffer, 0, buffer.length);
if (Read > 0) {
Fos.write (buffer, 0, read);
}
} while (read >-1);
Fos.close ();
Bis.close ();
Is.close ();
return fos.tostring (encoding);
}
}

160603. Tool class using Pd4ml.jar and Ss_css2.jar to PDF

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.