SharePoint 2013 运行时 "Could not load file or assembly 'Select.HtmlToPdf, Version=2.4.0.1" 错误。使用 Select.HtmlToPdf 第 3 方库

Error on runtime "Could not load file or assembly 'Select.HtmlToPdf, Version=2.4.0.1" on SharePoint 2013. Using Select.HtmlToPdf 3rd Party Library

我的错误

Could not load file or assembly 'Select.HtmlToPdf, Version=2.4.0.1, Culture=neutral, PublicKeyToken=e0ae9f6e27a97018' or one of its dependencies. The system cannot find the file specified.

到目前为止,我已经创建了我的 webpart 并通过此命令安装了 Select.HtmlToPdf 包 "PM> Install-Package Select.HtmlToPdf " (https://www.nuget.org/packages/Select.HtmlToPdf/)

我在部署时没有错误,只有在 运行 时。尽管一切看起来都很好并且对我来说就位,但这个错误不会消失。我是不是做错了什么或遗漏了什么?

编辑:仅当我在 SharePoint 2013(Visual WebPart)

上尝试 运行 Select.HtmlToPdf 时才会出现此错误

我的代码:

using SelectPdf;
using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace downloadPDF.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetRecord();
            }
        }
        private bool startConversion = false;
        private void GetRecord()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                            new DataColumn("Name", typeof(string)),
                            new DataColumn("Country",typeof(string)) });
            dt.Rows.Add(1, "John Hammond", "United States");
            dt.Rows.Add(2, "Mudassar Khan", "India");
            dt.Rows.Add(3, "Suzanne Mathews", "France");
            dt.Rows.Add(4, "Robert Schidner", "Russia");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            startConversion = true;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (startConversion)
            {
                // get html of the page
                TextWriter myWriter = new StringWriter();
                HtmlTextWriter htmlWriter = new HtmlTextWriter(myWriter);
                base.Render(htmlWriter);

                // instantiate a html to pdf converter object
                HtmlToPdf converter = new HtmlToPdf();

                // create a new pdf document converting the html string of the page
                PdfDocument doc = converter.ConvertHtmlString(
                    myWriter.ToString(), Request.Url.AbsoluteUri);

                // save pdf document
                doc.Save(Response, false, "Sample.pdf");

                // close pdf document
                doc.Close();
            }
            else
            {
                // render web page in browser
                base.Render(writer);
            }
        }
    }
}

对于任何使用第 3 方库的 SharePoint WebPart,解决方案是将该库添加到 GAC:

单击 Package.package > 高级 > 添加 > 从现有程序集添加 > 选定 Select.HtmlToPdf.dll > 将部署目标选中为 GAC > 保存!

此处有更多详细信息:https://social.msdn.microsoft.com/Forums/office/en-US/0b9ef307-e15c-4bc1-b5c0-dfc7aef04900/deploying-third-party-dll-from-solution?forum=sharepointdevelopmentprevious

正如汤姆所说,我向 GAC 添加了 Select.PdfToHtml.dll 库。

“我尝试在 GAC 中添加 Select.HtmlToPdf.dll 文件,它非常有效。我双击 Package.package > 高级 > 添加 > 从现有程序集添加 > 选择我的 Select.HtmlToPdf.dll(Net20) > 检查部署目标为 GAC > 保存! 参考资料:add library to GAC Could not load file or assembly Select.HtmlToPdf

之后,我可以加载我的页面并查看我的内容。但是当我点击按钮时;我收到这个错误

"Conversion failure. Could not find 'Select.Html.dep'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code."

要解决此问题,请执行以下步骤:

firstly I copied my Select.Html.Dep file(Net20) to my 15 hive folder "\TEMPLATE\CONTROLTEMPLATES\SelectPDFTest\VisualWebPart1\Select.Html.Dep"

then I set my full path like this in Code:

"// create a new pdf document converting the html string of the page SelectPdf.GlobalProperties.HtmlEngineFullPath = @"C:\Program Files\Common Files\microsoft shared\Web Server Extensions\TEMPLATE\CONTROLTEMPLATES\SelectPDFTest\VisualWebPart1\Select.Html.dep"; PdfDocument doc = converter.ConvertHtmlString(myWriter.ToString(), Request.Url.AbsoluteUri);" Reference: Could not load file or assembly Select.HtmlToPdf

所以最后,我的代码如下所示:

using SelectPdf;
using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SelectPDFTest.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetRecord();
            }
        }
        private bool startConversion = false;
        private void GetRecord()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                            new DataColumn("Name", typeof(string)),
                            new DataColumn("Country",typeof(string)) });
            dt.Rows.Add(1, "John Hammond", "United States");
            dt.Rows.Add(2, "Mudassar Khan", "India");
            dt.Rows.Add(3, "Suzanne Mathews", "France");
            dt.Rows.Add(4, "Robert Schidner", "Russia");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            startConversion = true;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (startConversion)
            {
                // get html of the page
                TextWriter myWriter = new StringWriter();

                HtmlTextWriter htmlWriter = new HtmlTextWriter(myWriter);
                base.Render(htmlWriter);

                // instantiate a html to pdf converter object
                HtmlToPdf converter = new HtmlToPdf();

                // set css @media screen
                converter.Options.CssMediaType = HtmlToPdfCssMediaType.Screen;

                // create a new pdf document converting the html string of the page
                SelectPdf.GlobalProperties.HtmlEngineFullPath = @"C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\CONTROLTEMPLATES\SelectPDFTest\VisualWebPart1\Select.Html.dep";

                string css = "<link rel='stylesheet' href='/Style Library/css/main.css' media='screen' />   ";
                string pageHtml = css + myWriter.ToString();

                PdfDocument doc = converter.ConvertHtmlString(pageHtml, Request.Url.AbsoluteUri);

                // save pdf document
                doc.Save(Response, false, "Sample.pdf");

                // close pdf document
                doc.Close();

                startConversion = false;
            }
            else
            {
                // render web page in browser
                base.Render(writer);
            }
        }
    }
}