動態網頁,帶背景圖和圖片。現在只能轉出文本內容。
uj5u.com熱心網友回復:
求助大神,急。代碼如下:uj5u.com熱心網友回復:
有沒有,有沒有人回答我啊啊啊
uj5u.com熱心網友回復:
string fileNameWithOutExtention = HttpContext.Current.Server.MapPath("..\Content\pdf\" + Guid.NewGuid() + ".pdf");string wkhtmltopdfPath = HttpContext.Current.Server.MapPath("..\App_Start\wkhtmltopdf.exe");
if (string.IsNullOrEmpty(html))
{
return "false";
}
ProcessStartInfo si;
StringBuilder paramsBuilder = new StringBuilder();
paramsBuilder.Append("--page-size A4 ");
paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", "-", fileNameWithOutExtention);
si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = wkhtmltopdfPath;
si.Arguments = paramsBuilder.ToString(); si.UseShellExecute = false; si.RedirectStandardError = true; si.RedirectStandardInput = true;
using (var process = new Process())
{
process.StartInfo = si; process.Start();
using (var stream = process.StandardInput) {
byte[] buffer = Encoding.UTF8.GetBytes(html); stream.BaseStream.Write(buffer, 0, buffer.Length); stream.WriteLine();
}
process.WaitForExit();
}
if (File.Exists(fileNameWithOutExtention)) {
//把檔案讀進檔案流
FileStream fs = new FileStream(fileNameWithOutExtention, FileMode.Open);
byte[] file = new byte[fs.Length];
fs.Read(file, 0, file.Length);
fs.Close();
}
else { throw new Exception("檔案不存在!"); }
return fileNameWithOutExtention;
uj5u.com熱心網友回復:
樓主教教我
uj5u.com熱心網友回復:
九云圖檔案轉換介面, 支持word轉pdf、pdf轉網頁、ppt轉pdf、html轉pdf、html轉pdf等各種格式的檔案轉換,保留原有格式和布局,矢量縮放,簡單呼叫即可輕松實作在線轉換。想體驗的可以到九云圖官網看下uj5u.com熱心網友回復:
cef 直接保存為PDFuj5u.com熱心網友回復:
package com.htmlToPdf.cn;import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.UUID;
class wkthmltopdf {
// wkhtmltopdf在系統中的路徑
// private static final String toPdfTool = "D:\\wkhtmltox-0.12.5-1.mxe-cross-win64\\bin\\wkhtmltopdf.exe";
/**
* html轉pdf
*
* @param srcPath
* html路徑,可以是硬碟上的路徑,也可以是網路路徑
* @param destPath
* pdf保存路徑
* @return 轉換成功回傳true
*/
public static boolean convert(String srcPath, String destPath) throws IOException {
Process ps = Runtime.getRuntime().exec("where wkhtmltopdf.exe");
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(ps.getInputStream(), Charset.forName("GBK")));
//wkhtmltopdf在系統中的路徑
String path ="";
String text ="";
while ((text = br.readLine())!=null){
path =text;
}
File file = new File(destPath);
File parent = file.getParentFile();
// 如果pdf保存路徑不存在,則創建路徑
if (!parent.exists()) {
//true代表擁有讀寫權限
file.setWritable(true);
parent.mkdirs();
}
StringBuilder cmd = new StringBuilder();
if (!System.getProperty("os.name").contains("Windows")) {
// 非windows 系統
//toPdfTool = FileUtil.convertSystemFilePath("/home/ubuntu/wkhtmltox/bin/wkhtmltopdf");
}
cmd.append(path);
cmd.append(" ");
//頁眉下面的線
cmd.append(" --header-line");
//設定頁面上的邊距
cmd.append(" --margin-top 3cm ");
// (設定頁眉和內容的距離,默認0)
cmd.append(" --header-spacing 5 ");
//設定在中心位置的頁腳內容
cmd.append(" --footer-center 第[page]頁/共[topage]頁");
// * 顯示一條線在頁腳內容上)
cmd.append(" --footer-line");
// (設定頁腳和內容的距離)
cmd.append(" --footer-spacing 5 ");
//要轉換的URL鏈接地址
cmd.append(srcPath);
cmd.append(" ");
//pdf存放路徑
cmd.append(destPath);
boolean result = true;
try {
Process proc = Runtime.getRuntime().exec(cmd.toString());
HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());
error.start();
output.start();
proc.waitFor();
} catch (Exception e) {
System.out.println("pdf轉換出錯");
result = false;
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws IOException {
wkthmltopdf.convert("https://www.baidu.com", "D:\\test201905\\13.pdf");
}
}
親測可用
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/41623.html
標籤:C#
上一篇:觸發問題
