在azure中部署wkhtmltopdf.exe作为云服务

时间:2016-02-11 00:46:22

标签: c# azure pdf-generation wkhtmltopdf

我正在努力寻找在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?

欢迎任何帮助或想法

2 个答案:

答案 0 :(得分:0)

Azure网站不支持 whhtmltopdf,因为它被SandBox阻止,该工具执行了一些操作,这些操作覆盖了我们在SandBox的帮助下阻止的操作系统层。

我现在没有替代方案,此处提供了不受支持的框架列表。

Azure Sandbox

答案 1 :(得分:-1)

我建议你试试EvoPdf中的HTML to PDF Converter for Azure Websites。您可以管理自己的HTML到PDF Azure Cloud Service,并且可以在您的网站中使用HTML到PDF Library for .NET的所有功能。您可以在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();
}