PD4ML

PD4ML is an excellent tool which convert html to pdf, can support most of the standard html tag and css.

Can append string page $[page] of $[total] on page header/footer to display page number.

Java code:
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;
protected int userSpaceWidth = 780;

public void pd4mlTesting(){
	try {
		String filename = "test.pdf";
		replaceHtml();
		File fz = new File("D:/PD4ML/test.html");
		java.io.FileInputStream fis = new java.io.FileInputStream(fz);
		InputStreamReader isr = new InputStreamReader( fis, "UTF-8" ); 
		PD4ML pd4ml = new PD4ML();
		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);
   
		FileOutputStream fos = new FileOutputStream("D:/PD4ML/"+filename);
		
     String pageHeader = "";
     String pageFooter = "";
     if (null != pageHeader && !"".equals(pageHeader.trim())) {
			PD4PageMark headerMark = new PD4PageMark();
			headerMark.setAreaHeight(30);
			headerMark.setInitialPageNumber(0);
			headerMark.setPagesToSkip(1);
			if (pageHeader.toUpperCase().startsWith("<HTML>") && pageHeader.toUpperCase().endsWith("</HTML>")) {
				//need page header and is HTML format
				headerMark.setHtmlTemplate(pageHeader);
			} else {
				//need page header and is text format
//				headerMark.setTitleAlignment(PD4PageMark.LEFT_ALIGN);
				headerMark.setTitleTemplate(pageHeader);
			}
			pd4ml.setPageHeader(headerMark);
		}
		
		if (null != pageFooter && !"".equals(pageFooter.trim())) {
			PD4PageMark footerMark = new PD4PageMark();
			footerMark.setAreaHeight(30);
			footerMark.setInitialPageNumber(0);
//			footerMark.setPagesToSkip(1);
			if (pageFooter.toUpperCase().startsWith("<HTML>") && pageFooter.toUpperCase().endsWith("</HTML>")) {
				//need page header and is HTML format
				footerMark.setHtmlTemplate(pageFooter);
			} else {
				//need page header and is text format
//				headerMark.setTitleAlignment(PD4PageMark.RIGHT_ALIGN);
				footerMark.setTitleTemplate(pageFooter);
			}
			pd4ml.setPageFooter(footerMark);
		}
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("ccRecipientsName","ccRecipientsName");
		parameters.put("subject","subject");
		parameters.put("minutesPurposeCode","minutesPurposeCode");
		pd4ml.setDynamicParams(parameters);
		pd4ml.render(isr, fos);
			
		} catch (InvalidParameterException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
private void replaceHtml(){
	String path = "D:/PD4ML/test.html";
	try {
	  StringBuffer bs = new StringBuffer();
	  InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
	  BufferedReader br = new BufferedReader(isr);
	  String line = null;
	  while ((line = br.readLine()) != null) {
	   bs.append(line + "\n");
	   }
	   br.close();
	   String str = bs.toString();
	   if (str.indexOf("@@minutesContent@@") != -1) {
	       str = str.replaceAll("@@minutesContent@@", getTestString());
	   }
	   OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path));
	   BufferedWriter bw = new BufferedWriter(osw); 
	   bw.write(str);
	   bw.flush();
	   bw.close();
	  } catch (Exception e) {
	  }
}


HTML Template:

<html>
<body>
<table>
	<tr>
		<td>CC:</td>
		<td>$[ccRecipientsName]</td>
	</tr>
	<tr>
		<td>subject:</td>
		<td>$[subject]</td>
	</tr>
	<tr>
		<td>minutesPurposeCode:</td>
		<td>$[minutesPurposeCode]</td>
	</tr>
	<tr>
		<td style="vertical-align:top">Content:</td>
		<td>@@minutesContent@@</td>
	</tr>
	
</table>
</body>
</html>



你可能感兴趣的:(java,html,css,pd4ml,pdf)