3

I am working on upgrading a .NET application without changing its functionality. What I did so far is:

  • upgrading from mvc 2 to mvc 4
  • upgrading from framework 3.5 to 4.0
  • using visual studio 2015 instead of 2010
  • upgrading the custom made utilities and mvc the same way

Right now I am stuck at the pdf conversion in the custom utilities dll. The error I get is in the image below. It says:

ExportHtmlToPdfBytes failed for url http://localhost:4984/Rijopdracht.mvc/Print/861175: Conversion error: WebKit Navigation timeout.

Error message.

The link is available and accessible behind a proxy. The method ExportHtmlToPdfBytes looks like this:

        public byte[] ExportHtmlToPdfBytes(string url)
    {
        try
        {
            PdfConverter pdfConverter = CreatePdfConvertor();

            byte[] bytes = pdfConverter.GetPdfBytesFromUrl(url);

            return bytes;
        }
        catch (Exception ex)
        {                
            throw new WrapperException(ex, "ExportHtmlToPdfBytes failed for url {0}:", url);
        }
    }

During debugging I discovered that the program breaks when it calls the function GetPdfBytesFromUrl. After the program breaks, the proxy user I use to access the url, gets blocked. I found out that a user gets blocked if and only if the password has been typed wrong 6 or more times.

I triple checked the username and password that came from the appsettings, and they are correct. Can somebody tell me what is going on and how to solve this?

The complete class PdfUtilities where this function comes from is:

using ExpertPdf.HtmlToPdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Jumbo.Utilities
{
    public class PdfUtilities
    {
        public PdfUtilities() {
            FooterText = String.Empty;
            FitWidth = false;
            LeftMargin = 5;
            RightMargin = 5;
            TopMargin = 5;
            BottomMargin = 5;
            PdfCompressionLevel = PdfCompressionLevel.Normal;
            JpegCompressionLevel = 10;
        }

        public string FooterText { get; set; }
        public string AuthenticationUsername { get; set; }
        public string AuthenticationPassword { get; set; }
        public bool FitWidth { get; set; }
        public string BaseUrl { get; set; }
        public int LeftMargin { get; set; }
        public int RightMargin { get; set; }
        public int TopMargin { get; set; }
        public int BottomMargin { get; set; }
        public int JpegCompressionLevel { get; set; }
        public PdfCompressionLevel PdfCompressionLevel { get; set; }

        private PdfConverter CreatePdfConvertor()
        {
            PdfConverter pdfConverter = new PdfConverter();

            pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
            pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel;
            pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Landscape;
            pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = false;
            pdfConverter.PdfDocumentOptions.ShowHeader = true;
            pdfConverter.PdfDocumentOptions.ShowFooter = true;
            pdfConverter.PdfDocumentOptions.LeftMargin = LeftMargin;
            pdfConverter.PdfDocumentOptions.RightMargin = RightMargin;
            pdfConverter.PdfDocumentOptions.TopMargin = TopMargin;
            pdfConverter.PdfDocumentOptions.BottomMargin = BottomMargin;
            pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
            pdfConverter.PdfDocumentOptions.EmbedFonts = true;
            pdfConverter.PdfDocumentOptions.JpegCompressionLevel = JpegCompressionLevel;


            if (FitWidth)
            {
                pdfConverter.PageWidth = 0;
                pdfConverter.PdfDocumentOptions.FitWidth = true;
                pdfConverter.PdfDocumentOptions.StretchToFit = true;
            }
            else
                pdfConverter.PdfDocumentOptions.FitWidth = false;

            //pdfConverter.PageWidth = 0;
            pdfConverter.PdfDocumentOptions.ShowHeader = false;

            pdfConverter.PdfFooterOptions.FooterTextColor = System.Drawing.Color.Black;
            pdfConverter.PdfFooterOptions.DrawFooterLine = false;
            pdfConverter.PdfFooterOptions.PageNumberText = "Page";
            pdfConverter.PdfFooterOptions.ShowPageNumber = true;
            pdfConverter.PdfFooterOptions.FooterText = FooterText;

            if (!string.IsNullOrEmpty(AuthenticationUsername) && !string.IsNullOrEmpty(AuthenticationPassword))
            {
                pdfConverter.AuthenticationOptions.Username = AuthenticationUsername;
                pdfConverter.AuthenticationOptions.Password = AuthenticationPassword;
            }
            else
            {
                string username = SettingsUtilities.GetSetting<string>(GenericConstants.AS_PROXY_USERNAME, null);
                if (!String.IsNullOrEmpty(username))
                {
                    string password = SettingsUtilities.GetSetting<string>(GenericConstants.AS_PROXY_PASSWORD, null);
                    pdfConverter.AuthenticationOptions.Username = username;
                    pdfConverter.AuthenticationOptions.Password = password;
                }
            }

            pdfConverter.LicenseKey = "tZ6HlY2Vh4WBg5WDm4WVhoSbhIebjIyMjA==";

            return pdfConverter;
        }

        public byte[] ExportHtmlToPdfBytes(string url)
        {
            try
            {
                PdfConverter pdfConverter = CreatePdfConvertor();

                byte[] bytes = pdfConverter.GetPdfBytesFromUrl(url);

                return bytes;
            }
            catch (Exception ex)
            {                
                throw new WrapperException(ex, "ExportHtmlToPdfBytes failed for url {0}:", url);
            }
        }

        public byte[] ExportHtmlStringToPdfBytes(string html)
        {
            PdfConverter pdfConverter = CreatePdfConvertor();

            byte[] bytes;
            if (BaseUrl == null)
                bytes = pdfConverter.GetPdfBytesFromHtmlString(html);
            else
                bytes = pdfConverter.GetPdfBytesFromHtmlString(html, BaseUrl);

            return bytes;
        }

        public void SaveHtmlToPdf(string url, string saveLocation)
        {
            Logging.LogVerboseCaller(Logging.Cat.Utilities, "Start export to htm for " + url);
            byte[] contents = ExportHtmlToPdfBytes(url);
            Logging.LogVerboseCaller(Logging.Cat.Utilities, "End export. Start save to " + saveLocation);
            FileUtilities.SaveFile(saveLocation, contents);
            Logging.LogVerboseCaller(Logging.Cat.Utilities, "End save");
        }
    }
}
LAN
  • 55
  • 1
  • 9

1 Answers1

1

A colleague of mine was able to solve it.

First of all: if we needed the proxy, we shouldn't use the AuthenticationOptions. I believe these are meant to login with your ExpertPDF account. There are proxy attributes for the PdfConverter class that could be used for that.

But, that wasn't the problem. My problem was that I used a license for an earlier version. The error wasn't clear on this, but it was an invalid license key.

To test this, we commented out the license key. We automatically used the demo version that puts a watermark over the generated pdfs, but it told us that it was a wrong license key that messed it up.

LAN
  • 55
  • 1
  • 9