Sicos1977 / ChromiumHtmlToPdf

Convert HTML to PDF with a Chromium based browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Converting too much HTML content on an Ubuntu server will cause the conversion to fail.

cd-linxi opened this issue · comments

Sorry to bother you again.
Because it's been bothering me for a long time,Can you test this problem on your end?
image
This will throw an exception,Conversion failed

commented

I don't have an Ubuntu installation. And can you please paste your logging so I can see what the conversion is doing

I introduced the Nuget package into the project, without logging, and threw an exception directly.

image
The local test was fine, and there was an exception when it was placed on the server.

commented

Can you place the entire exception with the stacktrace... It is impossible to solve a problem with just an error and nothing more. The reason why there is logging in the library is to see what it is doing.

image

if (string.IsNullOrEmpty(printToPdfResponse.Result?.Stream)) throw new ConversionException("Conversion failed");

The problem raised here is that the stack cannot be printed on the server.

20220725180957.txt

I will provide you with the HTML file,Change the suffix to.html and test it on an Ubuntu server to reproduce the problem.

commented

What I said... I don't have an ubuntu server so I can't test that.

commented

What you can try is add this line above var printToPdfResponse ... and see what is returned by Chrome... Hope this gives a clue why the Stream handle is empty.

Add this line ABOVE var printToPdfResponse ... and put the compiled assembly on the ubuntu server

throw new Exception(result);

This way I hope to see what Chrome is sending back to the caller and hopefully that gives some kind of clue what is going wrong.

commented

This will also work

            if (string.IsNullOrEmpty(printToPdfResponse.Result?.Stream))
                throw new ConversionException($"Conversion failed ... did not get the expected response from Chrome, response '{result}'");
commented

According to the Chrome dev manuals we should get a json message like this:

https://chromedevtools.github.io/devtools-protocol/tot/IO/

image
Can you release a Nuget package with the most recent submission?

My local project is too large to be introduced into your project, so I can only introduce it through Nuget. Could you please release the Nuget package?

image
I got an abnormal return on my test.
{"id":7,"error":{"code":-32000,"message":"Printing failed"}}

commented

I have to figure that one out hope there is something on Google dev that explains that problem

commented

I still guess it is a right problem because in a previous logging I saw a message complaining about writing to local "disk".

What you could try in the meanwhile is set the userProfile dir to the same location (but then in a sub directory) as where you are placing the html file that you want to convert.

Using another userProfile dir can be done through the constructor

        #region Constructor & Destructor
        /// <summary>
        ///     Creates this object and sets it's needed properties
        /// </summary>
        /// <param name="chromeExeFileName">When set then this has to be the full path to the chrome executable.
        ///     When not set then then the converter tries to find Chrome.exe by first looking in the path
        ///     where this library exists. After that it tries to find it by looking into the registry</param>
        /// <param name="userProfile">
        ///     If set then this directory will be used to store a user profile.
        ///     Leave blank or set to <c>null</c> if you want to use the default Chrome user profile location
        /// </param>
        /// <param name="logger">When set then logging is written to this ILogger instance for all conversions at the Information log level</param>
        /// <param name="useCache">When <c>true</c> (default) then Chrome uses it disk cache when possible</param>
        /// <exception cref="FileNotFoundException">Raised when <see cref="chromeExeFileName" /> does not exists</exception>
        /// <exception cref="DirectoryNotFoundException">
        ///     Raised when the <paramref name="userProfile" /> directory is given but does not exists
        /// </exception>
        public Converter(string chromeExeFileName = null,
                         string userProfile = null,
                         ILogger logger = null,
                         bool useCache = true)
        {
commented

Also another question... do you use any page margins?

image
No margins are used.

image
The userProfile I specified is also invalid.
The following image shows the exception with the userProfile specified.
image

commented

You are on your own with these errors, since I don't have a docker ubuntu container overhere I can't check those things

I have solved this problem caused by the Chrome running environment in Docker.

commented

Can you please explain what you did so that other people also can learn from it when they hit the same problem.

@cd-linxi Can you please explain how you fixed this? I have the same problem.

@cd-linxi Can you please explain how you fixed this? I have the same problem.

You need to add a browser parameter

Can you please explain what you did so that other people also can learn from it when they hit the same problem.
converter.AddChromeArgument("--disable-dev-shm-usage");
Add this Google browser parameter.

Add this Google browser parameter.converter.AddChromeArgument("--disable-dev-shm-usage");

Turn off /dev/shm usage, which will force Chrome to use the/TMP directory, although this will slow down execution because disk will be used instead of memory

This parameter uses local/ TMP instead of /dev/shm as the running space of Chrome. Local/TMP has more space than /dev/shm. Therefore, memory overflow is not easy to occur due to the large number of test cases in a file

@cd-linxi It worked! Thank you very much!

commented

Can you give me the exact error you got in the docker container so that I can add it to the readme for other people?

I didn't have any specific errors. When the ConvertToPdf method was called, the chrome process started but did not respond and did not throw any exceptions. I had to go into docker container and kill the chrome process manually, otherwise it stays open. This only happened when the HTML content was exceeded a certain size. As @cd-linxi said, space in /dev/shm was not enough.