Azure 网站和辅助角色 html 到 pdf

Azure Website and Worker Role html to pdf

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

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

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

任何人都知道如何配置辅助角色以使用 Azure 网站。

我在网上找不到太多关于此的信息。可能吗?

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

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

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

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

这是一个非常好用的API,只需安装专用的nuget包:

PM> Install-Package RotativaHQ

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

return new ViewAsPdf(model);

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

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

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

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

网站上的详细信息:

https://rotativahq.com