EvoPDF 库在 Windows Azure 托管站点上失败



EvoPDF HTML到PDF转换库(http://www.evopdf.com/)声称它支持Windows Azure云平台,但我无法让它工作。我得到例外:

[Exception: Could not get conversion result header. Data receive error. Could not receive data. Error code: 109]
   EvoPdf.HtmlToPdf.ImgConverter.GetLayoutFromUrl(String url, ps& htmlParseInfo) +622
   EvoPdf.HtmlToPdf.PdfConverter.ConvertAndGetPdfDocument(String url, String htmlString, String baseUrl, String internalLinksDocUrl, Boolean fromUrl) +9748
   EvoPdf.HtmlToPdf.PdfConverter.ConvertAndSaveToStream(Stream outStream, String url, String htmlString, String baseUrl, String internalLinksDocUrl, Boolean fromUrl) +61
   EvoPdf.HtmlToPdf.PdfConverter.SavePdfFromUrlToStream(String url, Stream outPdfStream) +20

这看起来像是在库通过 Web 请求获取 HTML 内容时失败的。Azure 中是否有任何内容可以阻止传出的 Web 请求?

该库部署为两个 DLL(一个本机 DLL 和一个托管程序集) - 是否需要任何特殊的 Azure 配置才能允许加载本机 DLL?(该库确实支持 xcopy 部署,我让它在其他托管环境中以这种方式工作)。

据我所知,您需要使用和Azure Web Role,而不是Azure网站。这些站点不支持以完全信任运行。

http://blogs.msdn.com/b/silverlining/archive/2012/06/27/windows-azure-websites-web-roles-and-vms-when-to-use-which.aspx

EvoPdf有一个

Azure示例项目,你可以下载该项目,该项目展示了如何使用可以运行EvoPdf dll的站点设置Web角色。

包含.dat文件

您是否也复制/包含 evointernal.dat 到网站?这为我解决了错误 109。它应与 *.dll 文件位于同一文件夹中。

背景:DLL + DAT文件

EVO HTML 到 PDF 库由三个文件组成:

  • evohtmltopdf.dll
  • evohtmltopdf.xml
  • 内在.dat

请注意,在更新 DLL 后,还要更新 evointernal.dat。否则,109 错误将再次出现。

如果问题与您的本机 DLL 相关,您可能需要尝试更改 ServiceDefinition.csdef 中的以下属性:

  • enableNativeCodeExecution:确保将其设置为 true(true 是默认值)
  • 执行上下文:尝试将其更改为提升

您是否部署了 64 位版本的 DLL?

我还在错误消息中看到"错误代码:109",您不能联系 EvoPDF 询问这是什么意思吗?

更新:如果您在 ASP.NET 中运行,您可以尝试更改信任级别吗?

<configuration>
  <system.web>
    <trust level="Full" />
  </system.web>
</configuration>

EVO HTML to PDF General Library for .NET 直接在 Azure Web Roles、Azure Worker Role 和 Azure Virtual Machines 中工作。

对于 Azure 网站

,您可以使用 EVO HTML to PDF for Azure 网站解决方案。从那里复制的代码示例是:

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Get the server IP and port
    String serverIP = textBoxServerIP.Text;
    uint serverPort = uint.Parse(textBoxServerPort.Text);
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort);
    // Set optional service password
    if (textBoxServicePassword.Text.Length > 0)
        htmlToPdfConverter.ServicePassword = textBoxServicePassword.Text;
    // Set HTML Viewer width in pixels which is the equivalent in converter of the browser window width
    htmlToPdfConverter.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text);
    // Set HTML viewer height in pixels to convert the top part of a HTML page 
    // Leave it not set to convert the entire HTML
    if (htmlViewerHeightTextBox.Text.Length > 0)
        htmlToPdfConverter.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text);
    // Set PDF page size which can be a predefined size like A4 or a custom size in points 
    // Leave it not set to have a default A4 PDF page
    htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize();
    // Set PDF page orientation to Portrait or Landscape
    // Leave it not set to have a default Portrait orientation for PDF page
    htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation();
    // Set the maximum time in seconds to wait for HTML page to be loaded 
    // Leave it not set for a default 60 seconds maximum wait time
    htmlToPdfConverter.NavigationTimeout = int.Parse(navigationTimeoutTextBox.Text);
    // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
    // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
    if (conversionDelayTextBox.Text.Length > 0)
        htmlToPdfConverter.ConversionDelay = int.Parse(conversionDelayTextBox.Text);
    // The buffer to receive the generated PDF document
    byte[] outPdfBuffer = null;
    if (convertUrlRadioButton.Checked)
    {
        string url = urlTextBox.Text;
        // Convert the HTML page given by an URL to a PDF document in a memory buffer
        outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
    }
    else
    {
        string htmlString = htmlStringTextBox.Text;
        string baseUrl = baseUrlTextBox.Text;
        // Convert a HTML string with a base URL to a PDF document in a memory buffer
        outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlString, baseUrl);
    }
    // Send the PDF as response to browser
    // Set response content type
    Response.AddHeader("Content-Type", "application/pdf");
    // Instruct the browser to open the PDF file as an attachment or inline
    Response.AddHeader("Content-Disposition", String.Format("{0}; filename=Getting_Started.pdf; size={1}",
        openInlineCheckBox.Checked ? "inline" : "attachment", outPdfBuffer.Length.ToString()));
    // Write the PDF document buffer to HTTP response
    Response.BinaryWrite(outPdfBuffer);
    // End the HTTP response and stop the current page processing
    Response.End();
}

最新更新