Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hanging TuesPechkin after initial conversion

I am attempting to create a Web API that can convert a styled HTML file into a PDF.

I am using TuesPechkin and have installed my application into IIS (as a 32-bit app: I have modified the application pool to run in 32bit mode).

IIS 8.5 is running on Windows Server 2012 R2.

PDFConversion class in C#:

using System.Drawing.Printing;
using System.IO;
using TuesPechkin;

namespace PDFApi
{
    public class PDFcreator
    {
        public void convert(string path, string uri)
        {
            IConverter converter = new StandardConverter(
                new RemotingToolset<PdfToolset>(
                    new Win64EmbeddedDeployment(
                    new TempFolderDelpoyment())));

            var document = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = true,
                    DocumentTitle = "Converted Form",
                    PaperSize = PaperKind.A4,
                    Margins =
                    {
                        All = 1.375,
                        Unit = Unit.Centimeters
                    }
                },
                Objects =
                {
                    new ObjectSettings { RawData = File.ReadAllBytes(uri) }
                }
            };

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

            FileStream fs = new FileStream(path, FileMode.Create);
            fs.Write(pdfBuf, 0, pdfBuf.Length);
            fs.Close();
        }
    }
}

The Controller is as follows:

    [Route("converthtml")]
    [HttpPost]
    [MIMEContentFilter]
    public async Task<HttpResponseMessage> ConvertHtml()
    {
        string temppath = System.IO.Path.GetTempPath();

        var streamProvider = new MultipartFormDataStreamProvider(temppath);
        await Request.Content.ReadAsMultipartAsync(streamProvider);


        string filepath = streamProvider.FileData.Select(entry => entry.LocalFileName.Replace(temppath + "\\", "")).First<string>();
        string pdfpath = System.IO.Path.GetTempFileName();
        pdfpath = pdfpath.Substring(0, pdfpath.LastIndexOf('.')) + ".pdf";

        new PDFcreator().convert(pdfpath, filepath);

        var stream = new FileStream(pdfpath, FileMode.Open);
        var result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        return result;
    }

Here's where it gets a little odd: Experimenting in Fiddler, sending a file once will return the PDF immediately. However, all subsequent POSTs will leave Fiddler hanging. Examining Task Manager shows the CPU and Memory for this task to jump up to 13.5% and ~96MB respectively.

The Temp folder (where the files are stored), on a successful run, will have three files in it: the original uploaded file (stored with a GUID-like name), the file generated via wkHtmlToPdf (in the form "wktemp-"), and the generated pdf (as tempXXXX.pdf). In the case of it hanging, only the first file can be found, indicating that the problem is somewhere in wkHtmlToPdf itself.

However, the real kicker is when the process is manually killed in Task Manager, the API completes successfully, returns the pdf, fully created!

If IIS is reset, the process returns to the original state; a new attempt will work without issue.

Obviously, resetting IIS after every call is hardly viable, nor is manually killing the process each time...

Is this a bug / are there any solutions to this issue?

like image 969
BM- Avatar asked Jan 20 '15 03:01

BM-


2 Answers

            var tempFolderDeployment = new TempFolderDeployment();
            var win32EmbeddedDeployment = new Win32EmbeddedDeployment(tempFolderDeployment);
            var remotingToolset = new RemotingToolset<PdfToolset>(win32EmbeddedDeployment);

            var converter =
                 new ThreadSafeConverter(remotingToolset);


            byte[] pdfBuf = converter.Convert(document);
            remotingToolset.Unload();

Unload remotingToolset will prevent hanging RemotingToolset.Unload();

like image 194
Toshkata Tonev Avatar answered Sep 23 '22 03:09

Toshkata Tonev


Very important-

@toshkata-tonev answer helped me, but when a lot of sessions used this function our server crushed because over CPU!

The point is that the process should be shared by all sessions as static shared function.

So this was the solution for me:

Implementation: Create a static class in your application:

public static class TuesPechkinInitializerService {
    private static string staticDeploymentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wkhtmltopdf");

public static void CreateWkhtmltopdfPath()
    {
        if (Directory.Exists(staticDeploymentPath) == false)
        {
            Directory.CreateDirectory(staticDeploymentPath);
        }
    }

    public static IConverter converter = 
        new ThreadSafeConverter(
            new RemotingToolset<PdfToolset>(
                new Win64EmbeddedDeployment(
                    new StaticDeployment(staticDeploymentPath)
                )
            )
        );
}

In GLOBAL.asax, I initialize that class on project start:

TuesPechkinInitializerService.CreateWkhtmltopdfPath();

And to use it:

HtmlToPdfDocument pdfDocument = new HtmlToPdfDocument
                {
                    GlobalSettings = new GlobalSettings(),
                    Objects =
                    {
                        new ObjectSettings
                        { 
                            ProduceLocalLinks = true,
                            ProduceForms = true,
                            HtmlText = htmlContent
                        }                    
                    }
                };

                byte[] pdfDocumentData = TuesPechkinInitializerService.converter.Convert(pdfDocument);

Thanks to:

https://github.com/tuespetre/TuesPechkin/issues/152

like image 41
Zvi Redler Avatar answered Sep 23 '22 03:09

Zvi Redler