HTML to PDF Converter for WinRT and Window Phone

winrt_logoEVO HTML to Converter for WinRT and Windows Phone is a portable .NET library that can be used in Windows Store Applications for Windows and for Windows Phone.

EVO HTML to PDF Converter library for WinRT can be easily integrated in Windows Store applications to convert HTML documents to PDF, raster images or SVG vector images. The library is a powerful tool helping you to instantly create nicely formatted and easily maintainable PDF reports directly from existing HTML reports.

The converter offers full support for HTML5, CSS3, JavaScript, SVG, web fonts, page breaks control with CSS and from API, automatically repeated HTML table header and footer, live URLs and internal links, automatically generated hierarchical bookmarks and table of contents, automatically generated fillable PDF forms and allows you to digitally sign and password protect the generated PDF documents.

The library was designed and tested to work reliably in multithreaded environments which makes it ideal for usage in high traffic websites and services.

In order to use the EVO HTML to PDF Converter for WinRT and Windows Phone you first have to install the EVO HTML to PDF Server. The server was built on .NET library to extend its capabilities to other platforms and languages. The portable client library that you link in your Windows and Windows Phone applications will connect to the server to convert HTML to PDF, to Image or to SVG.

EVO HTML to PDF Converter Server can run either in a Windows Service on a Windows machine or in an Azure Cloud Service deployed in Microsoft Azure cloud. You can find detailed installation and uninstallation instructions in the Readme.txt file from the root of the downloaded package.

You can download theĀ HTML to PDF Converter for WinRT and Windows Phone Package from product web page.

The EVO HTML to PDF Converter for WinRT API allows you to convert a HTML document to PDF in just a few lines a code. The programming interface is also very rich and allows you customize the generated PDF document in various ways. The code below is copied from the demo for HTML to PDF Converter that you can find the in the Demo folder of the software Zip package.

private async void buttonConvertUrlToPdf_Click(object sender, RoutedEventArgs e)
{
    // If another conversion is in progress then ignore current request
    bool ignoreRequest = false;
    lock(pendingConversionSync)
    {
        if (pendingConversion)
            ignoreRequest = true;
        else
        {
            msgUrlToPdfInProgress.Visibility = Windows.UI.Xaml.Visibility.Visible;
            pendingConversion = true;
        }
    }

    if (ignoreRequest)
        return;

    try
    {
        String serverIP = textBoxServerIP.Text;
        uint port = uint.Parse(textBoxServerPort.Text);

        HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(serverIP, port);
                
        // set license key
        htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

        // set service password if necessary
        if (textBoxServicePassword.Text.Length > 0)
            htmlToPdfConverter.ServicePassword = textBoxServicePassword.Text;

        // set HTML viewer width
        htmlToPdfConverter.HtmlViewerWidth = int.Parse(textBoxHtmlViewerWidth.Text);

        // set HTML viewer height if necessary
        if (textBoxHtmlViewerHeight.Text.Length > 0)
            htmlToPdfConverter.HtmlViewerHeight = int.Parse(textBoxHtmlViewerHeight.Text);

        // set navigation timeout
        htmlToPdfConverter.NavigationTimeout = int.Parse(textBoxHtmlViewerWidth.Text);

        // set conversion delay if necessary
        if (textBoxConversionDelay.Text.Length > 0)
            htmlToPdfConverter.ConversionDelay = int.Parse(textBoxConversionDelay.Text);

        // set PDF page size
        htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize();

        // set PDF page orientation
        htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation();

        // set margins
        htmlToPdfConverter.PdfDocumentOptions.LeftMargin = int.Parse(textBoxLeftMargin.Text);
        htmlToPdfConverter.PdfDocumentOptions.RightMargin = int.Parse(textBoxRightMargin.Text);
        htmlToPdfConverter.PdfDocumentOptions.TopMargin = int.Parse(textBoxTopMargin.Text);
        htmlToPdfConverter.PdfDocumentOptions.BottomMargin = int.Parse(textBoxBottomMargin.Text);
                
        // add header
        if (checkBoxAddHeader.IsChecked != null && (bool)checkBoxAddHeader.IsChecked)
        {
            htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true;
            DrawHeader(htmlToPdfConverter, true);
        }

        // add footer
        if (checkBoxAddFooter.IsChecked != null && (bool)checkBoxAddFooter.IsChecked)
        {
            htmlToPdfConverter.PdfDocumentOptions.ShowFooter = true;
            DrawFooter(htmlToPdfConverter, true, true);
        }

        string urlToConvert = textBoxUrl.Text;
        string errorMessage = null;

        // Convert the HTML page from give URL to PDF in a buffer
        byte[] pdfBytes = await Task.Run<byte[]>(() =>
        {
            byte[] resultBytes = null;
            try
            {
                resultBytes = htmlToPdfConverter.ConvertUrl(urlToConvert);
            }
            catch (Exception ex)
            {
                errorMessage = String.Format("Conversion failed. {0}", ex.Message);
                return null;
            }

            return resultBytes;
        });

        if (pdfBytes == null)
        {
            MessageDialog errorMessageDialog = new MessageDialog(errorMessage, "Conversion failed");
            await errorMessageDialog.ShowAsync();
            return;
        }

        // Save the PDF in a file
        Windows.Storage.StorageFolder installedLocation = Windows.Storage.ApplicationData.Current.LocalFolder;
        StorageFile outStorageFile = installedLocation.CreateFileAsync("EvoHtmlToPdf.pdf", CreationCollisionOption.ReplaceExisting).AsTask().Result;
        FileIO.WriteBytesAsync(outStorageFile, pdfBytes).AsTask().Wait();

        // Open the file in a PDF viewer
        await Windows.System.Launcher.LaunchFileAsync(outStorageFile);
    }
    finally
    {
        lock (pendingConversionSync)
        {
            msgUrlToPdfInProgress.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            pendingConversion = false;
        }
    }
}