HTML轉PDF總結
阿新 • • 發佈:2019-02-03
這兩天自己嘗試了一下,HTML頁面轉化成 PDF ,歷時2天,下面總結一下經驗:
1.先把某大神的關鍵程式碼貼出來
import java.awt.Dimension;
import java.awt.Insets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
/**
* @author jcoder
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class htmlToPdfServices {
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 runConverter(String urlstring, String fileName,
HttpServletResponse response) throws IOException {
if (urlstring.length() > 0) {
if (!urlstring.startsWith("http://")
&& !urlstring.startsWith("file:")) {
urlstring = "http://" + urlstring;
}
ByteArrayOutputStream ba = new ByteArrayOutputStream();
if (proxyHost != null && proxyHost.length() != 0 && proxyPort != 0) {
System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("proxyHost", proxyHost);
System.getProperties().setProperty("proxyPort", "" + proxyPort);
}
PD4ML pd4ml = new PD4ML();
pd4ml.setPageSize(new java.awt.Dimension(450, 450));
pd4ml.setPageInsets(new java.awt.Insets(20, 50, 10, 10));
pd4ml.enableImgSplit(false);
//pd4ml.useTTF("java:cn/com/pdf/fonts", true);
pd4ml.useTTF("java:fonts", true);
pd4ml.enableDebugInfo();
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);
URL url = new URL(urlstring);
pd4ml.render(urlstring, ba);
if(fileName.lastIndexOf(".pdf")==-1)
fileName = fileName + ".pdf";
try {
response.setHeader("Content-disposition",
"attachment; filename="
+ new String(fileName.getBytes("gb2312"),
"iso8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/pdf");
response.setContentLength(ba.size());
try {
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
out.close();
ba.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
這個是轉化的核心程式碼,沒看懂具體怎麼轉,但是能用就行!
光有這個還用不起來,需要引入兩個包 pd4ml_demo.jar, ss_css2.jar -
然後在專案的src 資料夾下面新建一個資料夾,把pd4fonts.properties、songti.ttf 檔案放在裡面 ,程式碼中有用到
做了這些 還是不夠,頁面上的中文會亂碼,需要在頁面中增加一個東西:
要在樣式中增加一個
*{
font-family: KaiTi_GB2312;
}
到這裡,基本就差不多啦! 接下來看呼叫的程式碼 :
htmlToPdfServices jt = new htmlToPdfServices();
jt.runConverter(url, name, response);
url: 就是要生成的PDF的網路路徑
name: 就是要儲存成PDF的名稱,這個可以隨便起
這個功能最最給力的還是,它可以彈出框讓使用者選擇輸出PDF的儲存路徑,這個很牛逼!
接下來總結 JS 頁面跳轉到servlet 的一些知識:
function store_searchToHTML(){
//這句話是獲得這個頁面的http地址
var url=window.location.href; //下面這句話是組裝呼叫Servlet的地址並傳遞引數多個引數 用 “&” 連線
surl= $("base").attr("href")+"/servlet/HtmlToPDFServlet?url="+url+"&name="+"巡店報告單";
//然後用以下方法去跳轉到Servlet中去
window.location.href =surl;
}
接下來還有一種跳轉,就是在頁面上直接跳轉
<a href="<%=basePath%>/servlet/TestServlet">匯出PDF</a>
好了,今天就總結這些,今天是2015年的最後一天! 2016,繼續加油!
1.先把某大神的關鍵程式碼貼出來
import java.awt.Dimension;
import java.awt.Insets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
/**
* @author jcoder
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class htmlToPdfServices {
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 runConverter(String urlstring, String fileName,
HttpServletResponse response) throws IOException {
if (urlstring.length() > 0) {
if (!urlstring.startsWith("http://")
&& !urlstring.startsWith("file:")) {
urlstring = "http://" + urlstring;
}
ByteArrayOutputStream ba = new ByteArrayOutputStream();
if (proxyHost != null && proxyHost.length() != 0 && proxyPort != 0) {
System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("proxyHost", proxyHost);
System.getProperties().setProperty("proxyPort", "" + proxyPort);
}
PD4ML pd4ml = new PD4ML();
pd4ml.setPageSize(new java.awt.Dimension(450, 450));
pd4ml.setPageInsets(new java.awt.Insets(20, 50, 10, 10));
pd4ml.enableImgSplit(false);
//pd4ml.useTTF("java:cn/com/pdf/fonts", true);
pd4ml.useTTF("java:fonts", true);
pd4ml.enableDebugInfo();
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);
URL url = new URL(urlstring);
pd4ml.render(urlstring, ba);
if(fileName.lastIndexOf(".pdf")==-1)
fileName = fileName + ".pdf";
try {
response.setHeader("Content-disposition",
"attachment; filename="
+ new String(fileName.getBytes("gb2312"),
"iso8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/pdf");
response.setContentLength(ba.size());
try {
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
out.close();
ba.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
這個是轉化的核心程式碼,沒看懂具體怎麼轉,但是能用就行!
光有這個還用不起來,需要引入兩個包 pd4ml_demo.jar, ss_css2.jar
然後在專案的src 資料夾下面新建一個資料夾,把pd4fonts.properties、songti.ttf 檔案放在裡面 ,程式碼中有用到
做了這些 還是不夠,頁面上的中文會亂碼,需要在頁面中增加一個東西:
要在樣式中增加一個
*{
font-family: KaiTi_GB2312;
}
到這裡,基本就差不多啦! 接下來看呼叫的程式碼 :
htmlToPdfServices jt = new htmlToPdfServices();
jt.runConverter(url, name, response);
url: 就是要生成的PDF的網路路徑
name: 就是要儲存成PDF的名稱,這個可以隨便起
這個功能最最給力的還是,它可以彈出框讓使用者選擇輸出PDF的儲存路徑,這個很牛逼!
接下來總結 JS 頁面跳轉到servlet 的一些知識:
function store_searchToHTML(){
//這句話是獲得這個頁面的http地址
var url=window.location.href; //下面這句話是組裝呼叫Servlet的地址並傳遞引數多個引數 用 “&” 連線
surl= $("base").attr("href")+"/servlet/HtmlToPDFServlet?url="+url+"&name="+"巡店報告單";
//然後用以下方法去跳轉到Servlet中去
window.location.href
}
接下來還有一種跳轉,就是在頁面上直接跳轉
<a href="<%=basePath%>/servlet/TestServlet">匯出PDF</a>
好了,今天就總結這些,今天是2015年的最後一天! 2016,繼續加油!