Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

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

Open
rajeshmuraleedharan opened this issue Nov 26, 2020 · 15 comments

Comments

@rajeshmuraleedharan
Copy link

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

@micahosborne
Copy link

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

@micahosborne
Copy link

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

@Groumph
Copy link

Groumph commented Nov 26, 2020

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();
    }

@micahosborne
Copy link

micahosborne commented Nov 26, 2020

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);
            }
        }

@Michael-List
Copy link

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)?

@kpeinado
Copy link

kpeinado commented Dec 7, 2020

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.

@danilobreda
Copy link

@fjsosa
Copy link

fjsosa commented Dec 29, 2020

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

@michael-nee
Copy link

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.

@yakeer
Copy link

yakeer commented Jan 12, 2021

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

@schallb
Copy link

schallb commented Jan 12, 2021

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

@yakeer
Copy link

yakeer commented Jan 12, 2021

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?

@michael-nee
Copy link

michael-nee commented Jan 17, 2021

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?

@schallb
Copy link

schallb commented Jan 25, 2021

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...

@fpanaccia
Copy link
Owner

Hi guys, i dont know if you have been following this issue fpanaccia/Wkhtmltopdf.NetCore-deprecated#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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests