使用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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章