我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
当前回答
2018年的更新,让我们使用标准的HTML+CSS=PDF方程式!
对于html到pdf的需求,有一个好消息。正如这个答案所示,W3C标准css-break-3将解决这个问题……这是一份候选人推荐书,计划在2017年或2018年经过测试后成为正式推荐书。
由于不太标准,有一些解决方案,使用c#插件,如print-css.rocks所示。
其他回答
与Winnovative HTML到PDF转换器,您可以转换HTML字符串在单行
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlString, baseUrl);
基URL用于解析HTML字符串中相对URL引用的图像。另外,你也可以在HTML中使用完整的url,或者使用src="data:image/png"作为图像标签嵌入图像。
在回答'fubaar'用户对Winnovative转换器的评论时,有必要进行更正。转换器不使用IE作为渲染引擎。它实际上不依赖于任何安装的软件,并且渲染与WebKit引擎兼容。
对于所有在。net 5及以上版本中寻找工作解决方案的人,请访问这里。
以下是我的工作解决方案。
使用wkhtmltopdf:
从这里下载并安装最新版本的wkhtmltopdf。 使用下面的代码。
public static string HtmlToPdf(string outputFilenamePrefix, string[] urls,
string[] options = null,
string pdfHtmlToPdfExePath = @"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")
{
string urlsSeparatedBySpaces = string.Empty;
try
{
//Determine inputs
if ((urls == null) || (urls.Length == 0))
throw new Exception("No input URLs provided for HtmlToPdf");
else
urlsSeparatedBySpaces = String.Join(" ", urls); //Concatenate URLs
string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name
var p = new System.Diagnostics.Process()
{
StartInfo =
{
FileName = pdfHtmlToPdfExePath,
Arguments = ((options == null) ? "" : string.Join(" ", options)) + " " + urlsSeparatedBySpaces + " " + outputFilename,
UseShellExecute = false, // needs to be false in order to redirect output
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
WorkingDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
}
};
p.Start();
// read the output here...
var output = p.StandardOutput.ReadToEnd();
var errorOutput = p.StandardError.ReadToEnd();
// ...then wait n milliseconds for exit (as after exit, it can't read the output)
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
// if 0 or 2, it worked so return path of pdf
if ((returnCode == 0) || (returnCode == 2))
return outputFilename;
else
throw new Exception(errorOutput);
}
catch (Exception exc)
{
throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, exc);
}
}
并调用上述方法HtmlToPdf("test", new string[] {"https://www.google.com"}, new string[] {"-s A5"}); 如果你需要将HTML字符串转换为PDF,调整上述方法,并将参数替换为进程StartInfo为$@"/C echo | set /p=""{htmlText}"" | ""{pdfHtmlToPdfExePath}""{((选项== null) ?"":字符串。加入(“”,选项))}-”“C: \用户桌面\ xxxx \ \ {outputFilename}”“”;
这种方法的缺点:
wkhtmltopdf的最新版本不支持最新的HTML5和CSS3。因此,如果您尝试将任何html导出为CSS GRID,那么输出将不会像预期的那样。 您需要处理并发性问题。
使用铬无头:
从这里下载并安装最新的chrome浏览器。 使用下面的代码。
var p = new System.Diagnostics.Process()
{
StartInfo =
{
FileName = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
Arguments = @"/C --headless --disable-gpu --run-all-compositor-stages-before-draw --print-to-pdf-no-header --print-to-pdf=""C:/Users/Abdul Rahman/Desktop/test.pdf"" ""C:/Users/Abdul Rahman/Desktop/grid.html""",
}
};
p.Start();
// ...then wait n milliseconds for exit (as after exit, it can't read the output)
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
这将把html文件转换为pdf文件。 如果你需要将一些url转换为pdf,那么使用下面的参数来处理StartInfo
@"/C——headless——disable-gpu——run-all- composter - stagebefore -draw——print-to-pdf-no-header——print-to-pdf=""C:/Users/Abdul Rahman/Desktop/test.pdf"" ""https://www.google.com"",
这种方法的缺点:
这与最新的HTML5和CSS3特性一样。输出将与您在浏览器中查看的相同,但当通过IIS运行时,您需要在LocalSystem Identity下运行应用程序的AppliactionPool,或者您需要提供对IISUSRS的读写访问。
使用Selenium WebDriver:
安装Nuget包硒。WebDriver和Selenium.WebDriver.ChromeDriver。 使用下面的代码。
public async Task<byte[]> ConvertHtmlToPdf(string html)
{
var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), "ApplicationName");
Directory.CreateDirectory(directory);
var filePath = Path.Combine(directory, $"{Guid.NewGuid()}.html");
await File.WriteAllTextAsync(filePath, html);
var driverOptions = new ChromeOptions();
// In headless mode, PDF writing is enabled by default (tested with driver major version 85)
driverOptions.AddArgument("headless");
using var driver = new ChromeDriver(driverOptions);
driver.Navigate().GoToUrl(filePath);
// Output a PDF of the first page in A4 size at 90% scale
var printOptions = new Dictionary<string, object>
{
{ "paperWidth", 210 / 25.4 },
{ "paperHeight", 297 / 25.4 },
{ "scale", 0.9 },
{ "pageRanges", "1" }
};
var printOutput = driver.ExecuteChromeCommandWithResult("Page.printToPDF", printOptions) as Dictionary<string, object>;
var pdf = Convert.FromBase64String(printOutput["data"] as string);
File.Delete(filePath);
return pdf;
}
该方法的优点:
这只需要一个Nuget安装和工作与最新的HTML5和CSS3功能预期。输出将与您在浏览器中查看的相同。
这种方法的缺点:
这种方法需要在应用程序运行的服务器上安装最新的chrome浏览器。 如果服务器中的chrome浏览器版本更新,则需要更新Selenium.WebDriver.ChromeDriver Nuget包。否则,由于版本不匹配,将抛出运行时错误。
使用这种方法,请确保在.csproj文件中添加<PublishChromeDriver>true</PublishChromeDriver>,如下所示:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<PublishChromeDriver>true</PublishChromeDriver>
</PropertyGroup>
这将在发布项目时发布chrome驱动程序。
这是我的工作项目repo - HtmlToPdf的链接
使用JavaScript中的window.print()从浏览器生成PDF
如果用户从浏览器使用你的应用程序,那么你可以依靠JavaScript和使用window.print()和必要的打印媒体css从浏览器生成PDF。例如,生成发票从浏览器在一个库存应用程序。
该方法的优点:
不依赖于任何工具。 在浏览器中直接从HTML, CSS和JS生成PDF。 快 支持所有最新的CSS属性。
这种方法的缺点:
在像Blazor这样的SPA中,我们需要使用iframe来打印页面的部分。
在几乎花了2天的时间与可用的选项之后,我得到了上面的答案,最终实现了基于Selenium的解决方案,它正在工作。希望这能帮到你,节省你的时间。
在c#中转换HTML到PDF使用ABCpdf。
ABCpdf可以使用Gecko或Trident呈现引擎,因此您的HTML表看起来与FireFox和Internet Explorer中的相同。
在www.abcpdfeditor.com上有一个ABCpdf的在线演示。您可以使用它来检查您的表将如何首先呈现,而不需要下载和安装软件。
为了渲染整个网页,你需要AddImageUrl或AddImageHtml函数。但如果你想做的只是添加HTML样式的文本,那么你可以尝试AddHtml函数,如下所示:
Doc theDoc = new Doc();
theDoc.FontSize = 72;
theDoc.AddHtml("<b>Some HTML styled text</b>");
theDoc.Save(Server.MapPath("docaddhtml.pdf"));
theDoc.Clear();
ABCpdf是一个商业软件,但是标准版通常可以在特别优惠下免费获得。
到目前为止,似乎最好的免费。net解决方案是TuesPechkin库,它是wkhtmltopdf本机库的包装。
我现在已经使用单线程版本将几千个HTML字符串转换为PDF文件,它似乎工作得很好。它应该也可以在多线程环境中工作(例如IIS),但我还没有对此进行测试。
另外,因为我想使用最新版本的wkhtmltopdf(在编写时为0.12.5),我从官方网站下载了DLL,复制到我的项目根目录,设置copy to output为true,并像这样初始化库:
var dllDir = AppDomain.CurrentDomain.BaseDirectory;
Converter = new StandardConverter(new PdfToolset(new StaticDeployment(dllDir)));
上面的代码看起来完全是“wkhtmltox.dll”,所以不要重命名文件。我使用的是64位版本的DLL。
确保你阅读了多线程环境的说明,因为你只需要在每个应用生命周期中初始化它一次,所以你需要把它放在一个单例或其他东西中。
如果你想让用户在浏览器中下载渲染页面的pdf,那么最简单的解决方案是
window.print();
在客户端,它将提示用户保存当前页面的PDF。您还可以通过链接样式自定义pdf的外观
<link rel="stylesheet" type="text/css" href="print.css" media="print">
css在打印时应用于HTML。
限制
不能将文件存储在服务器端。 用户提示打印页面时,必须手动保存页面。 页必须在选项卡中呈现。