Wkhtmltopdf.NetCore.Example-deprecated icon indicating copy to clipboard operation
Wkhtmltopdf.NetCore.Example-deprecated copied to clipboard

QPainter::begin(): Returned false Exit with code 1, due to unknown error. .net 5 docker linux

Open rajeshmuraleedharan opened this issue 4 years ago • 15 comments

Getting "QPainter::begin(): Returned false Exit with code 1, due to unknown error." when I use .Net 5 Linux docker container.

Working fine with Aspnet Core 3.1 version. Please help me to resolve this issue

This is the error image

Docker File image

rajeshmuraleedharan avatar Nov 26 '20 00:11 rajeshmuraleedharan

I'm having the same issue, mine worked find in 3.1, any progress on the issue @rajeshmuraleedharan ?

micahosborne avatar Nov 26 '20 21:11 micahosborne

The odd thing is that i can bash into the container and run the same exact commands then it will output the pdf to the console output. But running through dotnet processstartinfo it fails in .net 5. Makes me feel like its some kind of change in processes standard output etc

micahosborne avatar Nov 26 '20 21:11 micahosborne

The error exists in the proc code. It seems that something has changed in .net 5 or in the docker images used by .net 5. The standard in/out is throwing the error.

private static byte[] Convert(IWkhtmltopdfPathProvider pathProvider, string switches, string html) {

        // generate PDF from given HTML string, not from URL
        if (!string.IsNullOrEmpty(html))
        {
            switches += " -";
            html = SpecialCharsEncode(html);
        }

        var wkhtmlPath = pathProvider.GetPath();
        using var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = wkhtmlPath,
                Arguments = switches,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                CreateNoWindow = true
            }
        };

        try
        {
            proc.Start();
        }
        catch (Exception e)
        {
            throw new WkhtmlDriverException($"Failed to start wkhtmltodpf at path {wkhtmlPath}.", e);
        }

        // generate PDF from given HTML string, not from URL
        if (!string.IsNullOrEmpty(html))
        {
            using (var sIn = proc.StandardInput)
            {
                sIn.WriteLine(html);
            }
        }

        using var ms = new MemoryStream();
        using (var sOut = proc.StandardOutput.BaseStream)
        {
            byte[] buffer = new byte[4096];
            int read;

            while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
        }

        string error = proc.StandardError.ReadToEnd();

        if (ms.Length == 0)
        {
            throw new Exception(error);
        }

        proc.WaitForExit();

        return ms.ToArray();
    }

Groumph avatar Nov 26 '20 21:11 Groumph

Yeah im using this same code, i changed to storing a temp file and it works. So it has something to do with the standardoutput code

        using var ms = new MemoryStream();
        using (var sOut = proc.StandardOutput.BaseStream)
        {
            byte[] buffer = new byte[4096];
            int read;

            while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
        }

micahosborne avatar Nov 26 '20 22:11 micahosborne

I'm having the same issue. Has anyone tried if it makes a difference if you run it on linux (not in a docker container)?

Michael-List avatar Dec 01 '20 19:12 Michael-List

I'm having the same issue. Has anyone tried if it makes a difference if you run it on linux (not in a docker container)?

I have my web site in centos7 and I'm getting the same issue.

kpeinado avatar Dec 07 '20 03:12 kpeinado

Same Issue here, CentOS 7, no docker, netcore 5

fjsosa avatar Dec 29 '20 04:12 fjsosa

Yeah im using this same code, i changed to storing a temp file and it works. So it has something to do with the standardoutput code

        using var ms = new MemoryStream();
        using (var sOut = proc.StandardOutput.BaseStream)
        {
            byte[] buffer = new byte[4096];
            int read;

            while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
        }

How did you get this output to a temp file? Nothing works for me.

michael-nee avatar Jan 04 '21 18:01 michael-nee

Facing the same issue. will appreciate an hint for a solution.=

yakeer avatar Jan 12 '21 08:01 yakeer

Hello,

I found a fix that works well, in short: i let wkhtmltopdf write directly to a file (in case of Linux) The file is read after the process is done and returned.

Change has to be done WkhtmlDriver.cs (see attachment) In addition: RazorViewToStringRenderer.cs has to be adopted to get a valid action context Wkhtmltopdf.NetCore.zip

schallb avatar Jan 12 '21 12:01 schallb

Hello,

I found a fix that works well, in short: i let wkhtmltopdf write directly to a file (in case of Linux) The file is read after the process is done and returned.

Change has to be done WkhtmlDriver.cs (see attachment) In addition: RazorViewToStringRenderer.cs has to be adopted to get a valid action context Wkhtmltopdf.NetCore.zip

Really hope this will work. Can you make a PR?

yakeer avatar Jan 12 '21 14:01 yakeer

In addition: RazorViewToStringRenderer.cs has to be adopted to get a valid action context

Tried this and getting issues with the path of the pdf /app/generatedPdf not found. Assume the pdf is going somewhere else. What linux image are you using?

michael-nee avatar Jan 17 '21 20:01 michael-nee

Finally i use a combination of Rotativa.AspNetCore and Wkhtmltopdf.NetCore. Wkhtmltopdf.NetCore has some ugly glitches but the better driver integration. Since I do not know on which repo to push it's hard to judge what to do...

schallb avatar Jan 25 '21 10:01 schallb

Hi guys, i dont know if you have been following this issue https://github.com/fpanaccia/Wkhtmltopdf.NetCore/issues/46, but i just pushed a dirty and above all temporary solution https://www.nuget.org/packages/Wkhtmltopdf.NetCore/5.0.2-preview, as i said in the issue, i would recommend that only implement this version if you really really really need it, writing files in containers is not very optimal.

When net 5.0.4 i will push a final fix for this + some new features

fpanaccia avatar Feb 22 '21 14:02 fpanaccia