1

I want to throw an exception if a web page that TuesPechkin is generating for PDF returns HTTP Error 404 or 500. I don't want to get a PDF file for the friendly-looking HTML error page.

It have tried the EventHandler (see the codes below), but none of them capture the error code or error message. The only time ErrorEvent is raised is when I pass in a URL with bogus domain name, like http://www.google.bogus

Pechkin has class property called 'HttpErrorCode' in SynchronizedPechkin. Where is it in TuesPechkin?

public ActionResult Index()
{
    var messages = new StringBuilder();

    IConverter converter =
        new ThreadSafeConverter(
            new RemotingToolset<PdfToolset>(
                new Win64EmbeddedDeployment(
                    new TempFolderDeployment())));

    var obj = new ObjectSettings();
    obj.PageUrl = "http://www.google.com/dummy";

    var doc = new HtmlToPdfDocument();
    doc.Objects.Add(obj);

    converter.Error += new EventHandler<ErrorEventArgs>(converter_Error);
    converter.Warning += new EventHandler<WarningEventArgs>(converter_Warning);
    converter.PhaseChange += new EventHandler<PhaseChangeEventArgs>(converter_PhaseChange);
    converter.Begin += new EventHandler<BeginEventArgs>(converter_Begin);

    byte[] pdfBinary = null;
    try
    {
        pdfBinary = converter.Convert(doc);
    }
    catch (Exception e)
    {
        messages.AppendLine(e.Message);
    }
    finally
    {
        converter.Error -= new EventHandler<ErrorEventArgs>(converter_Error);
        converter.Warning -= new EventHandler<WarningEventArgs>(converter_Warning);
        converter.PhaseChange -= new EventHandler<PhaseChangeEventArgs>(converter_PhaseChange);
        converter.Begin -= new EventHandler<BeginEventArgs>(converter_Begin);
    }

    if (messages.Length > 0)
    {
        throw new ApplicationException(messages.ToString());
    }
    return File(pdfBinary, "application/pdf");
}


void converter_Begin(object send, BeginEventArgs e)
{
    messages.AppendLine(e.ToString());
}

void converter_PhaseChange(object send, PhaseChangeEventArgs e)
{
    messages.AppendLine(e.PhaseDescription);
}

void converter_Warning(object send, WarningEventArgs e)
{
    messages.AppendLine(e.WarningMessage);
}

void converter_Error(object sender, ErrorEventArgs e)
{
    messages.AppendLine(e.ErrorMessage);
}
Tony
  • 1,827
  • 1
  • 22
  • 23
  • Hi @Tony, we are experiencing the same issue re a friendly 404 error is the output of a PDF file produced. Did you ever find a solution to getting a more detailed output to debug the PDF creation issue? – David Jun 23 '21 at 10:39
  • 1
    @David I ended up calling the command-line program wkhtmltopdf and check the return code. – Tony Jul 14 '21 at 20:32
  • Thanks @Tony, for some reason upon redeploying the same code our issue resolved itself, but good to know if it happens again – David Jul 16 '21 at 10:58

0 Answers0