我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
当前回答
到目前为止,似乎最好的免费。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。
确保你阅读了多线程环境的说明,因为你只需要在每个应用生命周期中初始化它一次,所以你需要把它放在一个单例或其他东西中。
其他回答
我之前也在找这个。我遇到了HTMLDOC http://www.easysw.com/htmldoc/,这是一个免费的开源命令行应用程序,它以HTML文件为参数,并从中输出PDF。这在我的副业项目中非常有效,但这完全取决于你真正需要什么。
制作它的公司出售编译后的二进制文件,但您可以免费从源代码下载和编译并使用它。我设法编译了一个最近的版本(1.9版本),我打算在几天内发布它的二进制安装程序,所以如果你感兴趣,我可以在发布它时立即提供它的链接。
编辑(2/25/2014):看起来文档和网站转移到http://www.msweet.org/projects.php?Z1
你也可以检查Spire,它允许你用这段简单的代码创建HTML到PDF
string htmlCode = "<p>This is a p tag</p>";
//use single thread to generate the pdf from above html code
Thread thread = new Thread(() =>
{ pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
// Save the file to PDF and preview it.
pdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");
OpenHtmlToPdf是一个免费的库,使用起来非常简单
string timeStampForPdfName = DateTime.Now.ToString("yyMMddHHmmssff");
string serverPath = System.Web.Hosting.HostingEnvironment.MapPath("~/FolderName");
string pdfSavePath = Path.Combine(@serverPath, "FileName" + timeStampForPdfName + ".FileExtension");
//OpenHtmlToPdf Library used for Performing PDF Conversion
var pdf = Pdf.From(HTML_String).Content();
//FOr writing to file from a ByteArray
File.WriteAllBytes(pdfSavePath, pdf.ToArray()); // Requires System.Linq
如果你想让用户在浏览器中下载渲染页面的pdf,那么最简单的解决方案是
window.print();
在客户端,它将提示用户保存当前页面的PDF。您还可以通过链接样式自定义pdf的外观
<link rel="stylesheet" type="text/css" href="print.css" media="print">
css在打印时应用于HTML。
限制
不能将文件存储在服务器端。 用户提示打印页面时,必须手动保存页面。 页必须在选项卡中呈现。
对于所有在。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的解决方案,它正在工作。希望这能帮到你,节省你的时间。