使用Select.HtmlToPdf.NetCore 把html内容生成pdf文件
引用类库
通过nuget查找Select.HtmlToPdf.NetCore并引用
控制器使用
自己写一个页面,或者找个可用的网站都可以测试
代码如下
/// <summary>
/// 导出pdf
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> Export(string url)
{
SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf();
//converter.Options.PdfPageOrientation = SelectPdf.PdfPageOrientation.Landscape;
SelectPdf.PdfDocument doc = converter.ConvertUrl(url);//"http://localhost:5020/PRINT/DyColdStoragePrint"
var path = "wwwroot/pdf/" + DateTime.Now.ToString("yyyyMMdd") + "/test.pdf";
doc.Save(path);
doc.Close();
return Content(path.Replace("wwwroot/", ""));
}
遇到问题
只能在windows上使用,无法跨平台,会报错
Conversion failure. Unable to load shared library 'kernel32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libkernel32.dll: cannot open shared object file: No such file or directory
System.Exception: Conversion failure. Unable to load shared library 'kernel32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libkernel32.dll: cannot open shared object file: No such file or directory
at SelectPdf.HtmlToImage.ᜀ(String A_0, ឭ[]& A_1, ជ& A_2, String& A_3)
at SelectPdf.HtmlToPdf.ᜂ(String A_0, String A_1, String A_2, String A_3, Boolean A_4)
at SelectPdf.HtmlToPdf.ConvertUrl(String url)
官方表明只能支持windows,比较遗憾
https://github.com/selectpdf/selectpdf-free-html-to-pdf-converter/issues/2