Join GitHub today
Make background transparent in generated PDF (instead of white). #2084
Previously, wkhtmltopdf rendered a white background explicitly. This makes it impossible to use the generated PDF as an overlay for another PDF (merging the PDFs using other tools). This patch removes the default-white background. Note that it is still possible to force the white background using <body style="background: white">, but I don't think that is normally useful.
I understand your reasoning, but let me describe why I think it should not be an option:
It used to fill only the part of the page up to the bottom of the contents (i.e. everything below the bottom of the rendered content would be transparent; the rest would be filled white). I consider that to be undesirable/unexpected behavior, and think it should not be default and not an option.
The only reason I see why the option would exist would be for backwards compatibility when people rely on that behavior (probably a very small minority). Most likely, if they rely on it, they mean to use
It is up to you of course, but I think having an option for this would be over-conservative, unless I am missing some scenario (I admit that I'm not a (wkhtmlto)pdf expert).
I keep stumbling upon new details here, and it is getting more confusing...
In general, the body defines the size of the background that is drawn. The body width seems to be equal to the page width (minus margins), and the body height depends on the contents. All but the last page are filled with the background color. If the body height is not an exact multiple of the page height (again except margins), then the last page will have a transparent background in the bottom: the remaining part of the page below the body. This happens consistently with
My patch removes the white background layer in the transparent case and in the any color case, but does not add it in the white case. (I don't see how it could do that either; it seems to be a bug elsewhere.) With the patch, I thus end up with:
So it makes a little more sense, but that does make it impossible to get a truly white background...
As for screenshots, I uploaded a few PDFs instead: http://we.tl/USfZ13ElAe .
For the record, I used the following arguments:
PS. Interestingly, this means I can achieve what I wanted without any code changes, by setting
I uploaded the same files again at:
I tested it with the git HEAD version at that time, with and without my patch applied. The behavior seems to have changed with 0.13.
I uploaded a test case for reproduction here: http://vdtak.eu/files/wkhtmltopdf_transparency_test_2.zip
For anyone searching with how to get this to work unamended on 0.12, on Linux Ubuntu Trusty, here's what I've found.
On 0.12, I'm rendering with --no-background. By default, the following HTML draws a white background (even with --no-background):
Specifying a transparent background like this still produces a white background.
But specifying a white background produces a transparent background!
Therefore I respectively disagree about an option not being needed because the only way to get a transparent background at the moment appears to be to ask for a white background.
referenced this pull request
Jan 18, 2015
The default transparent background has an unfortunate effect on PDFs attached to emails and viewed on the Mac mail app. Mac mail renders the PDF's pages into the body of the email, but the transparency allows the background color of the email to show through. Setting
Here is the HTML used for the email body:
Here is the HTML used to generate the PDF:
Finally, the wkhtmltopdf command is