There are several ways to convert HTML files into PDF files on the Internet, but some have strict requirements on the HTML file format. If you are a little wrong, you cannot generate the PDF file we want. Here I recommend one
PD4ML, which can solve the problem of incorrect HTML file format. It can generate a good PDF file, which is fast in processing and highly compatible with CSS files. Below is the most basic
PD4ML programming:
Java code
Package samples;
Import java. awt. Insets;
Import java. io. File;
Import java. io. IOException;
Import java.net. MalformedURLException;
Import java.net. URL;
Import java. security. InvalidParameterException;
Import org. zefer. pd4ml. PD4Constants;
Import org. zefer. pd4ml. PD4ML;
Public class GettingStarted1 {
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 ){
Try {
GettingStarted1 jt = new GettingStarted1 ();
Jt. doConversion ("http://pd4ml.com/sample.h
Tm "," c:/pd4ml.pdf ");
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public void doConversion (String url, String outputPath)
Throws InvalidParameterException, MalformedURLException, IOException {
File output = new File (outputPath );
Java. io. FileOutputStream fos = new java. io. FileOutputStream (output );
PD4ML pd4ml = new PD4ML ();
Pd4ml. setHtmlWidth (userSpaceWidth); // set frame width of "virtual web browser"
// Choose target paper format and "rotate" it to landscape orientation
Pd4ml. setPageSize (pd4ml. changePageOrientation (PD4Constants. A4 ));
// Define PDF page margins
Pd4ml. setPageInsetsMM (new Insets (topValue, leftValue, bottomValue, rightValue ));
// Source HTML document also may have margins, cocould be suppressed this way
// (PD4ML * Pro * feature ):
Pd4ml. addStyle ("BODY {margin: 0}", true );
// If built-in basic PDF fonts are not sufficient or
// If you need to output non-Latin texts,
// TTF embedding feature shocould help (PD4ML * Pro *)
Pd4ml. useTTF ("c:/windows/fonts", true );
Pd4ml. render (new URL (url), fos); // actual document conversion from URL to file
Fos. close ();
System. out. println (outputPath + "\ ndone .");
}
}
The following Java class slightly changes the above example. now it pre-reads source HTML to a string and passes it torender () method wrapped toStringReader. first it writes PDF bytes toByteArrayOutputStream, which makes possible to measure size of the resulting document.
A disadvantage of the method is a bigger RAM utilization.
Java code
Package samples;
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;
Public class GettingStarted2 {
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 ){
Try {
GettingStarted2 jt = new GettingStarted2 ();
String html = readFile ("c:/sample.htm", "UTF-8 ");
Jt. doConversion2 (html, "c:/pd4ml.pdf ");
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public void doConversion2 (String htmlDocument, String outputPath)
Throws InvalidParameterException, MalformedURLException, IOException {
PD4ML pd4ml = new PD4ML ();
Pd4ml. setHtmlWidth (userSpaceWidth); // set frame width of "virtual web browser"
// Choose target paper format
Pd4ml. setPageSize (pd4ml. changePageOrientation (PD4Constants. A4 ));
// Define PDF page margins
Pd4ml. setPageInsetsMM (new Insets (topValue, leftValue, bottomValue, rightValue ));
// Source HTML document also may have margins, cocould be suppressed this way
// (PD4ML * Pro * feature ):
Pd4ml. addStyle ("BODY {margin: 0}", true );
// If built-in basic PDF fonts are not sufficient or
// If you need to output non-Latin texts, TTF embedding feature shocould help
// (PD4ML * Pro *)
Pd4ml. useTTF ("c:/windows/fonts", true );
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
// Actual document conversion from HTML string to byte array
Pd4ml. render (new StringReader (htmlDocument), baos );
// If the HTML has relative references to images etc,
// Use render () method with baseDirectory parameter instead
Baos. close ();
System. out. println ("resulting PDF size:" + baos. size () + "bytes ");
// In Web scenarios it is a good idea to send the size
// "Content-length" HTTP header
File output = new File (outputPath );
Java. io. FileOutputStream fos = new java. io. 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 );
}
}
Author: pingpang