import org.zefer.pd4ml.PD4ML; import org.zefer.pd4ml.PD4Constants; ... 2 protected Dimension format = PD4Constants.A4; protected boolean landscapeValue = false; protected int topValue = 10; protected int leftValue = 10; protected int rightValue = 10; protected int bottomValue = 10; protected String unitsValue = "mm"; protected String proxyHost = ""; protected int proxyPort = 0; 3 protected int userSpaceWidth = 780; ... private void runConverter(String urlstring, File output) throws IOException { if (urlstring.length() > 0) { if (!urlstring.startsWith("http://") && !urlstring.startsWith("file:")) { urlstring = "http://" + urlstring; } 4 java.io.FileOutputStream fos = new java.io.FileOutputStream(output); 5 if ( proxyHost != null && proxyHost.length() != 0 && proxyPort != 0 ) { System.getProperties().setProperty("proxySet", "true"); System.getProperties().setProperty("proxyHost", proxyHost); System.getProperties().setProperty("proxyPort", "" + proxyPort); } 6 PD4ML pd4ml = new PD4ML(); 7 try { pd4ml.setPageSize( landscapeValue ? pd4ml.changePageOrientation( format ): format ); } catch (Exception e) { e.printStackTrace(); } if ( unitsValue.equals("mm") ) { pd4ml.setPageInsetsMM( new Insets(topValue, leftValue, bottomValue, rightValue) ); } else { pd4ml.setPageInsets( new Insets(topValue, leftValue, bottomValue, rightValue) ); } pd4ml.setHtmlWidth( userSpaceWidth ); 8 pd4ml.render( urlstring, fos ); } } ...
Comments:
1. Import the PD4ML converter class
2. Define HTML-to-PDF converting parameter values if needed. See API reference for more info.
3. Specify user space width. It has an analogy to Web-browser window horizontal size. From common web-browsing experience you can guess, that changing of the size can affect the HTML document representation: HTML elements arrangement, vertical size etc. See API reference for more info.
4. Preparing output stream for PDF generation.
5. Specifying proxy settings if the source HTML document is behind the firewall.
6. Instantiating PD4ML converter.
7. Passing to it HTML-to-PDF converting parameters.
8. Performing HTML-to-PDF translation. Note: using of an URL is not mandatory. PD4ML can read a source HTML from input stream. See API reference for more info.