【问题标题】:Deploying wkhtmltopdf.exe in azure as cloud service在 azure 中部署 wkhtmltopdf.exe 作为云服务
【发布时间】:2016-05-21 14:40:14
【问题描述】:

我正在努力寻找在 azure 网站中从 HTML 生成 PDF 文件的最佳方法。我尝试了 iTextSharp,但它不能很好地格式化 pdf,也没有支持大多数 CSS3/HTML5 功能。 我还查看了下面给出的付费选项:

http://www.winnovative-software.com/html-to-pdf-converter-azure.aspx
http://www.hiqpdf.com/
http://www.evopdf.com/

所有这些都支持 CSS3/HTML5 的最新功能,但也需要在 Azure 上设置云服务,这基本上可以完成所有处理,因为 Azure 网站在受限环境中运行。

还有另一个免费的工具是 Wkhtmltopdf。我已经让它在我的本地工作,它在解析 HTML 方面做得很好。但我不太确定如何让那个作为 Azure 云服务运行? 我想我需要从 Visual Studio 生成一个包并将其部署到 Azure?

欢迎任何帮助或想法

【问题讨论】:

    标签: c# azure pdf-generation wkhtmltopdf


    【解决方案1】:

    whhtmltopdf 在 Azure 网站上不受支持,因为它被 SandBox 阻止,该工具会执行一些操作,以达到我们在 SandBox 的帮助下阻止的操作系统层。

    我现在没有其他选择,此处列出了不受支持的框架列表。

    Azure Sandbox

    【讨论】:

    【解决方案2】:

    我建议您尝试来自 EvoPdf 的 HTML to PDF Converter for Azure Websites。您可以管理自己的 HTML 到 PDF Azure 云服务,并且可以在您的网站中使用适用于 .NET 的 HTML 到 PDF 库的所有功能。您可以在 http://www.evopdf.com/clientdemo/ 找到包含所有功能的 C# 示例代码的演示应用程序。一个简单的 C# 代码示例是:

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-13
      • 2017-06-24
      • 2012-09-05
      • 2018-08-29
      • 2016-07-25
      • 1970-01-01
      • 2014-12-31
      • 2013-08-19
      • 2015-11-10
      相关资源
      最近更新 更多