简体   繁体   中英

Deploying wkhtmltopdf.exe in azure as cloud service

I am struggling to find a best way to generate PDF files from HTML in azure website. I tried iTextSharp but it doesn't format the pdf very well and also hasn't got support for most of the CSS3/HTML5 features. I also had a look at the paid options which are given below:

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

All of those one supports the latest features of CSS3/HTML5 but that also require s to setup the cloud service on Azure which would basically do all the processing as Azure websites runs in a restricted environment.

There is another tool which is Wkhtmltopdf which is available for free. I have got it working on my local and it does a good job in parsing the HTML. But I am not too sure how can I get that one running as Azure cloud service ? I think I would need to generate a package from the visual studio and deploy it to Azure ?

Any help or ideas are welcome

The whhtmltopdf is not supported on Azure Websites as it is prevented by the SandBox , the tool does some operations reaching out the OS layer which we prevent with the help of the SandBox.

I don't have an alternative for you right now, The list of unsupported frameworks are present here.

Azure Sandbox

I would recommend you to try the HTML to PDF Converter for Azure Websites from EvoPdf. You can manage your own HTML to PDF Azure Cloud Service and you can use in your website all the features of HTML to PDF Library for .NET. You can find a demo application with C# sample code for all the features at http://www.evopdf.com/clientdemo/ . A simple C# code sample is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM