4

I'm using TuesPechkin for my web application, which I'm testing locally on IIS with VS2013. The user clicks a button and the page's current HTML is saved to a PDF file, which is then emailed out. This process is going to be run regularly as the site's data changes.

When converter.Convert(document) first runs, it converts without problem. Every subsequent attempt, however, results in the process hanging and me needing to restart VS.

Below is some default code I've been using to test.

public void MakePDF()
{
    var document = new HtmlToPdfDocument
    {
        GlobalSettings =
        {
            ProduceOutline = true,
            DocumentTitle = "Pretty Websites",
            PaperSize = PaperKind.A4, // Implicit conversion to PechkinPaperSize
            Margins =
            {
                All = 1.375,
                Unit = TuesPechkin.Unit.Centimeters
            }
        },
        Objects = {
            new ObjectSettings { HtmlText = "<h1>Pretty Websites</h1><p>This might take a bit to convert!</p>" }
        }
    };

    IConverter converter =
        new ThreadSafeConverter(
            new PdfToolset(
                new Win32EmbeddedDeployment(
                    new TempFolderDeployment())));

    byte[] result = converter.Convert(document);
}

Can anyone point me in the right direction on this? Most of my troubleshooting so far has led to some discussions on threading and pooling, but no concrete code solutions for running TuesPechkin more than once.

Nic
  • 12,220
  • 20
  • 77
  • 105
MikeOShay
  • 522
  • 1
  • 7
  • 17

2 Answers2

3

Have you tried the ThreadSafeConverter? The StandardConverter is only suitable for small console apps.

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

byte[] result = converter.Convert(document);

Note that you should keep the converter somewhere static, or as a singleton instance (as mentioned here).

Nic
  • 12,220
  • 20
  • 77
  • 105
  • Changing that block of code resulted in the same problem. I've modified the question to include the containing method, which is currently just your run-of-the-mill public void. Are you saying I should move the converter-definition and the conversion itself to its own method, and pass `document` to it? – MikeOShay Apr 29 '15 at 01:50
  • 4
    Try moving the `converter` into a static class or singleton. Because of the dependency on wkhtmltopdf, it can only create a single `converter`, meaning you need to reuse it. – Nic Apr 29 '15 at 01:56
  • Note that your code is slightly different than mine. In the Tuespechkin FAQ, which I linked, it's mentioned that you need to use the `RemotingToolset` for IIS apps. Not sure if that affects your issue though. – Nic Apr 29 '15 at 01:57
  • You check ref code here [link](https://github.com/tloy1966/TuesPechkin/blob/0c3bc3673872985241cb29cd69c31786a9667e8e/Source/TuesPechkin.Wkhtmltox.AnyCPU/PDFHelper.cs#L23), in IIS apps, be sure use lock when getting the converter – Max CHien Aug 25 '19 at 07:19
0

Since this app on IIS, could get singleton converter, and use RemotingToolset

var toolSet = new RemotingToolset<PdfToolset>(winAnyCpuEmbeddedDeployment);

// Then

using TuesPechkin.Wkhtmltox.AnyCPU;
...
var converter = PDFHelper.Factory.GetConverter();
var result = converter.Convert(This.Document);

Reference : https://github.com/tloy1966/TuesPechkin

Max CHien
  • 133
  • 1
  • 8