Azure网站和工作者角色html到pdf

时间:2015-02-19 13:16:58

标签: c# asp.net pdf azure azure-worker-roles

我已经尝试了几个.NET pdf库来从html页面创建一个pdf页面。

在Azure中,它不适用于网站,因为我收到了超时。

我在网上发现人们正在谈论将pdf转换作为工作者角色。

任何人都知道如何配置工作人员角色以使用azure网站。

我无法在网上找到关于此的更多信息。有可能吗?

3 个答案:

答案 0 :(得分:1)

你把事情搞混了。 Azure网站是一项服务。 Azure辅助角色是在Cloud Service中运行的无状态虚拟机。它们是两个不同的东西。此外,您不需要需要工作者角色来生成PDF(尽管它当然是一个可行的选择)。您只需要能够安装PDF渲染软件,无论是在Windows还是在Linux中。

您将无法在Azure网站中安装此类软件,但可以在Azure Web /辅助角色(通过启动脚本)或虚拟机(通过ssh / rdp)中安装软件。您选择的以及您选择的PDF库完全取决于您(在此范围之外,因为该级别的架构是主观的)。

答案 1 :(得分:0)

EVO拥有solution for converting HTML to PDF in Azure Websites。以下是在Azure网站中从HTML创建PDF的代码段:

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

答案 2 :(得分:0)

我是Rotativa nuget包的作者。出于安全策略原因和操作系统限制,它无法在Azure网站上使用。为了解决这个问题,我在Azure上创建了一个SaaS版本。

这是一款非常易于使用的API,只需安装专用的nuget包:

PM> Install-Package RotativaHQ

从Razor Views创建PDF文件,就像:

一样简单
return new ViewAsPdf(model);

View / HTML中不需要任何特殊内容。在images / css / js链接中不需要绝对URL。也适用于localhost(开发机器)。

目前,该服务在4个Azure区域设有端点:美国东部,美国西部,欧盟北部,东南亚。

它很快,因为它使用专有协议将网页内容发送到API以转换为PDF。

它可靠,因为所有端点都是负载平衡的。

网站详情:

https://rotativahq.com

相关问题