DF與HTML是兩種不同格式的文件,PDF是一種常見的便攜式文檔,HTML是一種常見的網頁格式,這兩種看似相差甚遠的文件甚至可以轉換。一些網站編輯需要將存儲數據的PDF文件轉換為存儲數據的HTML網站內容,這種方便直接將HTML嵌入到網頁模板中。那么你知道PDF是如何轉換成HTML文檔的嗎?以下與您分享。
方法一:風云PDF軟件
HTML將需要轉換成HTMLPDF將文件上傳到相應的功能頁面,這個網站可以支持批量轉換,每個人都可以上傳多個文件;
在將PDF轉換為HTML之前,先選擇文件轉換的頁碼,其中可選擇轉換每一頁、奇數頁、偶數頁、指定頁;
此后可點擊“開始轉換”按鈕,文檔轉換成功后,可在瀏覽器上觀看相應的HTML文檔。
方法二:風云PDF在線網頁
在瀏覽器中打開風云PDF在線網站,并選擇PDF轉換到網站首頁的功能欄。HTML。
在線PDF轉換為HTML,PDF文檔可以直接轉換為HTML網頁格式,文檔成功轉換后,可以在瀏覽器上觀看HTML格式文檔,其原有的PDF頁面元素和排版都能準確保存。
方法三:WPS辦公組
文檔轉換成功后,其PDF頁面元素和排版都能準確保存;
PDF到HTML轉換速度快,精度高;
轉換過程中,操作簡單,使用方便。
以上是如何將PDF轉換成HTML文檔的相關內容,如果你對此感興趣,你可以學習,風云PDF在線網站不僅可以實現PDF和HTML轉換,還有很多不同格式文件的轉換哦!
司的某項業務需要與用戶線上簽訂協議,即用戶在線手寫一個簽名,后臺將公司公章信息和用戶的簽名以及合同信息生成一份PDF文件,供用戶查看和下載。
比對了一些插件,我們最終決定使用dompdf這個插件,插件的github在這里:https://github.com/dompdf/dompdf。
1. 使用方法
// 引入命名空間 use Dompdf\Dompdf; // 初始化dompdf對象 $dompdf=new Dompdf(); // 加載html文檔內容 $dompdf->loadHtml('hello world'); // 設置紙張類型和方向 $dompdf->setPaper('A4', 'landscape'); // 渲染HTML為PDF $dompdf->render(); // 流輸出 $dompdf->stream();
2. 常見問題和解決辦法
2.1 中文亂碼的問題
插件對于字體和編碼問題是這樣形容的:
PDF documents internally support the following fonts: Helvetica, Times-Roman, Courier, Zapf-Dingbats, & Symbol. These fonts only support Windows ANSI encoding. In order for a PDF to display characters that are not available in Windows ANSI, you must supply an external font. Dompdf will embed any referenced font in the PDF so long as it has been pre-loaded or is accessible to dompdf and reference in CSS @font-face rules. See the font overview for more information on how to use fonts.The DejaVu TrueType fonts have been pre-installed to give dompdf decent Unicode character coverage by default. To use the DejaVu fonts reference the font in your stylesheet, e.g. body { font-family: DejaVu Sans; } (for DejaVu Sans). The following DejaVu 2.34 fonts are available: DejaVu Sans, DejaVu Serif, and DejaVu Sans Mono.
嘗試了一下,默認帶的字體是無法渲染中文的,使用CSS的@font-face引入會報錯(也可能是我打開方式不對)。這樣就只好自己引入一個字體了。
插件給了一個安裝語言文件的工具,地址再這里:https://github.com/dompdf/utils。
使用步驟:
這樣,我們就可以在html文檔的css中使用font-family屬性來指定字體了。
html { font-family: simkai; }
2.2 圖片無法展示
插件應該是無法直接顯示網絡圖片,所以需要將圖片轉換為BASE64格式才能顯示。
將HTML文檔中的所有圖片轉換為BASE64的方式:
function imgToBase64($html) { $html=preg_replace_callback('/<img(?:.*?)src="(.*?)"(?:.*?)\/?>/', function($matches) { $imageInfo=getimagesize($matches[1]); $base64="" . chunk_split(base64_encode(file_get_contents($matches[1]))); $base64_image='data:' . $imageInfo['mime'] . ';base64,' . $base64; return str_replace($matches[1], $base64_image, $matches[0]); }, $html); return $html; }
這樣轉換其實性能影響挺大的,感覺性能不太好可以加一下緩存。
嘍,今天是一篇HTML to PDF速食指南。
Java 轉換 HTML 到PDF有許多類庫,今天我們介紹一下第三方免費的類庫OpenPDF。
OpenPDF是免費的Java類庫 ,遵從LGPL 和 MPL協議,所以基本上能夠可以隨意使用。OpenPDF是基于iTEXT的,目前來說也是維護的比較好的Java操作PDF的開源軟件。
話不多說,且看所需要的依賴,
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-core</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>1.0.6</version>
</dependency>
jsoup可以將html文件轉換成輸入流等,也可以遍歷html的DOM節點,提取元素及樣式等。
本篇示例將以下html文件轉換成pdf
<html>
<head>
<style>
.center_div {
border: 1px solid #404e94;
margin-left: auto;
margin-right: auto;
background-color: #f6d0ed;
text-align: left;
padding: 8px;
}
table {
width: 100%;
border: 1px solid black;
}
th, td {
border: 1px solid black;
}
body,html,input{font-family:"msyh";}
</style>
</head>
<body>
<div class="center_div">
<h1>Hello java North!</h1>
<div>
<p>convert html to pdf.</p>
</div>
<div>
<table>
<thead>
<th>ROLE</th>
<th>NAME</th>
<th>TITLE</th>
</thead>
<tbody>
<tr>
<td>MARKSMAN</td>
<td>ASHE</td>
<td>THE FROST ARCHER</td>
</tr>
<tr>
<td>MAGES</td>
<td>ANNIE</td>
<td>THE DARK CHILD</td>
</tr>
<tr>
<td>射手</td>
<td>凱塔琳</td>
<td>皮城女警</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
以上html用瀏覽器打開如下,亂碼是因為中文字體不識別,下面轉換的時候會加載對應的字體來進行轉換。
使用Java轉換HTML到PDF代碼如下:
public class HtmlToPDFOpenSource {
public static void main(String[] args) throws IOException {
HtmlToPDFOpenSource htmlToPDFOpenSource=new HtmlToPDFOpenSource();
htmlToPDFOpenSource.generatePdfByOpenhtmltopdf();
}
private void generatePdfByOpenhtmltopdf() throws IOException {
File inputHtml=new File("E:\\javaNorth\\java-study-note\\javaOpenSource\\src\\main\\resources\\test.html");
//加載html文件
Document document=Jsoup.parse(inputHtml, "UTF-8");
document.outputSettings().syntax(Document.OutputSettings.Syntax.html);
//引入資源目錄,可以單獨引入css,圖片文件等
String baseUri=FileSystems.getDefault()
.getPath("javaOpenSource\\src\\main\\resources")
.toUri().toString();
try (OutputStream os=new FileOutputStream("javaOpenSource\\src\\main\\resources\\testOpenLeagueoflegends1.pdf")) {
PdfRendererBuilder builder=new PdfRendererBuilder();
builder.withUri("javaOpenSource\\src\\main\\resources\\testOpenLeagueoflegends1.pdf");
builder.toStream(os);
builder.withW3cDocument(new W3CDom().fromJsoup(document), baseUri);
//引入指定字體,注意字體名需要和css樣式中指定的字體名相同
builder.useFont(new File("javaOpenSource\\src\\main\\resources\\fonts\\msyh.ttf"),"msyh",1,BaseRendererBuilder.FontStyle.NORMAL, true);
builder.run();
}
}
}
使用Java代碼轉換成PDF如下(示例中使用了微軟雅黑中文字體):
上述html文件中增加如下外部樣式:
<link href="style.css" rel="stylesheet">
并在resources目錄下添加style.css文件,重新生成PDF文件如下。
本片介紹了使用OpenPDF將html文件轉換成PDF文件。同時也使用了自定義字體,外部樣式。但是以下幾點需要格外注意。
全部示例在此:https://github.com/javatechnorth/java-study-note/tree/master/javaOpenSource/src/main/java/pdf
文章來源:Java技術指北
*請認真填寫需求信息,我們會在24小時內與您取得聯系。