代码之家  ›  专栏  ›  技术社区  ›  Kurt

Azure网站和工作人员角色html转pdf

  •  1
  • Kurt  · 技术社区  · 10 年前

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

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

    我在网上发现,人们正在谈论如何以工人的身份运行pdf转换。

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

    我在网上找不到关于这个的很多信息。有可能吗?

    3 回复  |  直到 10 年前
        1
  •  1
  •   David Makogon    10 年前

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

    您将无法在Azure网站中安装此类软件,但您 可以 在Azure web/worker角色(通过启动脚本)或虚拟机(通过ssh/rdp)中安装软件。你选择哪一个,以及你选择的PDF库,完全取决于你(这里不在范围之内,因为这一层次的架构是主观的)。

        2
  •  0
  •   EvoPdf    10 年前

    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();
    }
    
        3
  •  0
  •   Giorgio Bozio    9 年前

    我是Rotativa nuget软件包的作者。由于安全策略原因和操作系统限制,它不能在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

    推荐文章