Question

I'm writing a Windows 8 Store App using WinJS. My app needs to generate PDFs with text and graphs. I was under the impression that PDFtron could convert HTML to PDF, but that does not seem to be the case for an App Store application. Is this true?

The front end uses WinJS/HTML and Telerik Radcharts to render graphs in SVG. I then pipe the DOM down to disk as an HTML file. It shows the graph and numbers nicely. I want to convert the HTML to a PDF to preserve the styling as well as the content.

The WinRT version does not come with the HTML2PDF assembly or the .Convert() method. Is it somewhere else? I've searched the docs, samples, and the web.

Was it helpful?

Solution

PDFTron's PDFNet SDK on WinRT does not support HTML to PDF conversion (as at version 6.2).

Here is the response I received from PDFTron support on this question:

While the PDFNet SDK on WinRT cannot itself convert from HTML to PDF, the PDFNet SDK on Windows desktop can do so. You can find sample code for for HTML to PDF conversion at http://www.pdftron.com/pdfnet/samplecode.html#HTML2PDF.

Some of our clients send HTML to a server of theirs, where PDFNet can convert the HTML to PDF. Note that on Windows desktop there are many conversion options, including converting Office to PDF and converting any printable document format to PDF.

OTHER TIPS

EVO has implemented the following solution to convert HTML to PDF in WinRT and Windows Store Applications. You can find a compelte code sample in that page.

The copy of the code sample is:

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 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;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top