我知道这个工具会在 url 上查找并将响应转换为 pdf。我如何转换
<html> content.. </html>
成pdf?
我正在查看 wkhtml2pdf 上的帮助文件,看起来它提供了标准输入选项,但我真的不知道如何模拟标准输入。
另外,如果您知道一个做得更好的工具,请给我一些建议。
非常感谢!
我知道这个工具会在 url 上查找并将响应转换为 pdf。我如何转换
<html> content.. </html>
成pdf?
我正在查看 wkhtml2pdf 上的帮助文件,看起来它提供了标准输入选项,但我真的不知道如何模拟标准输入。
另外,如果您知道一个做得更好的工具,请给我一些建议。
非常感谢!
我刚刚开始了一个新项目,以提供围绕 wkhtmltopdf 的 C# P/Invoke 包装器。
您可以在以下位置查看我的代码:https ://github.com/pruiz/WkHtmlToXSharp
问候。
看看佩奇金
.NET Wrapper for WkHtmlToPdf DLL,使用 Webkit 引擎将 HTML 页面转换为 PDF 的库。
Nuget 包:
示例代码:
private void ConvertToPdf()
{
var loadPath = Server.MapPath("~/HtmlTemplates");
var loadFile = Path.Combine(loadPath, "Test.html");
var savePath = Server.MapPath("~/Pdf");
var saveFile = Path.Combine(savePath, DateTime.Now.ToString("HH-mm-ss.fff") + ".pdf");
var globalConfig = new GlobalConfig()
.SetMargins(0, 0, 0, 0)
.SetPaperSize(PaperKind.A4);
var pdfWriter = new SynchronizedPechkin(globalConfig);
pdfWriter.Error += OnError;
pdfWriter.Warning += OnWarning;
var objectConfig = new ObjectConfig()
.SetPrintBackground(true)
.SetIntelligentShrinking(false);
var pdfBuffer = pdfWriter.Convert(objectConfig, File.ReadAllText(loadFile));
File.WriteAllBytes(saveFile, pdfBuffer);
}
private void OnWarning(SimplePechkin converter, string warningtext)
{
throw new NotImplementedException();
}
private void OnError(SimplePechkin converter, string errortext)
{
throw new NotImplementedException();
}
wkhtmltopdf 是一个免费工具,但它不是用 .NET 编写的,它可能很难集成到您的 asp.net 应用程序中。
您可以查看iTextSharp,它是免费的,但不能处理任何类型的 html,或者您可以查看将 html 转换为 pdf 的商业工具,例如可以处理任何 html/css 的ExpertPDF或ABCpdf 。
我找到了一个方法。您可以设置另一个以输出正常的html。并使用该 url 作为 wkhtml2pdf 进程的输入值。
- - - - - 编辑
public byte[] WKHtmlToPdf(string url_input)
{
try
{
var fileName = " - ";
var wkhtmlDir = ConfigurationSettings.AppSettings["wkhtmlDir"];
var wkhtml = ConfigurationSettings.AppSettings["wkhtml"];
var p = new Process();
string url = Request.Url.GetLeftPart(UriPartial.Authority) + @"/application/" + url_input;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = wkhtml;
p.StartInfo.WorkingDirectory = wkhtmlDir;
string switches = "";
switches += "--print-media-type ";
switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
switches += "--page-size Letter ";
p.StartInfo.Arguments = switches + " " + url + " " + fileName;
p.Start();
//read output
byte[] buffer = new byte[32768];
byte[] file;
using (var ms = new MemoryStream())
{
while (true)
{
int read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
{
break;
}
ms.Write(buffer, 0, read);
}
file = ms.ToArray();
}
// wait or exit
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
return returnCode == 0 ? file : null;
}
catch (Exception ex)
{
// set your exceptions here
return null;
}
}
----------web.config 关键示例
<add key="wkhtmlDir" value="C:\Program Files (x86)\wkhtmltopdf\bin"/>
<add key="wkhtml" value="C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe"/>
基本思想是将 url 作为参数传递给 exe。
!
如果你想用 Css 打印 html 的特定部分
安装 Nuget 包:
佩奇金同步
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlprint.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
string cssPath = Server.MapPath("~/css/style1.css");
string cssString = File.ReadAllText(cssPath);
cssPath = Server.MapPath("~/css/bootstrap.css");
string cssString2 = File.ReadAllText(cssPath);
cssString += cssString2;
GlobalConfig gc = new GlobalConfig();
byte[] pdfContent = new SimplePechkin(new GlobalConfig()).Convert(@"<html><head><style>" + cssString + "</style></head><body>" + sw.ToString() + "</body></html>");
在这里您可以轻松了解wkhtmltopdf的工作原理 https://ourcodeworld.com/articles/read/366/how-to-generate-a-pdf-from-html-using-wkhtmltopdf-with-c-in-winforms