Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdout of wkhtmltopdf will be added to pdf #19

Closed
harald-k opened this issue Mar 21, 2017 · 7 comments
Closed

stdout of wkhtmltopdf will be added to pdf #19

harald-k opened this issue Mar 21, 2017 · 7 comments
Assignees
Labels
Milestone

Comments

@harald-k
Copy link

harald-k commented Mar 21, 2017

When running the following code on Linux, the standard output of wkhtmltopdf will be added to the created pdf. Without xvfb on Windows the created pdf is as expected.

Linux environment:
Ubuntu 14.04.3 LTS
wkhtmltopdf 0.12.2.1 (with patched qt)

Windows environment:
Windows 10
wkhtmltopdf 0.12.4 (with patched qt)

final XvfbConfig xc = new XvfbConfig();
xc.addParams(new Param("--auto-servernum"));

final WrapperConfig wc = new WrapperConfig();
wc.setXvfbConfig(xc);

Pdf pdf = new Pdf(wc);

pdf.addPage("http://...", PageType.url);

pdf.addParam(new Param("--orientation", "landscape"));

Files.copy(new ByteArrayInputStream(pdf.getPDF()), outputFile);

Created PDF

Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
[=======>                                                    ] 12%
[==========>                                                 ] 17%
[=============>                                              ] 22%
[===============>                                            ] 25%
[================>                                           ] 27%
[=======================>                                    ] 39%
[=========================>                                  ] 42%
[==========================>                                 ] 44%
[============================>                               ] 47%
[=============================>                              ] 49%
[==============================>                             ] 50%
[==============================>                             ] 50%
[==============================================>             ] 78%
[=================================================>          ] 83%
[===================================================>        ] 86%
[============================================================] 100%
Counting pages (2/6)                                               
[============================================================] Object 1 of 1
Resolving links (4/6)                                                       
[============================================================] Object 1 of 1
Loading headers and footers (5/6)                                           
Printing pages (6/6)
[>                                                           ] Preparing
[>                                                           ] Page 1 of 60
%PDF-1.4
1 0 obj
<<
[...]
@jhonnymertz jhonnymertz self-assigned this Mar 22, 2017
@jhonnymertz jhonnymertz added this to the 1.1.0.RELEASE milestone Mar 22, 2017
@stepan-sadovnykov
Copy link

It is possible to use
pdf.addParam(new Param("-q"));
as a workaround, this parameter hides most of wkhtmltopdf messages.
In my case without this some PDFs produced were unreadable.

@jhonnymertz
Copy link
Owner

Sorry for the late reply.
So, I was not able to reproduce the bug. The code works for me on the described Linux environment. It is hard to say where the bug is happening though. It can be in the wrapper streams or in the wkhtmltopdf itself.

Please, can you check if the command generated by the wrapper can be properly used on the command line? You can use the method getCommand() on the class Pdf.

@kalle77
Copy link

kalle77 commented Jul 4, 2017

I seem to be running into the exact same problem on:
Ubuntu 15.04 &
wkhtmltopdf 0.12.4 (with patched qt)
Running in a webapp on tomcat 8.0.14

The command im running:
wkhtmltopdf --page-size A4 --header-html http://localhost:8080/digitaldocs/headerView.htm?userAccountId=39&projectId=31609 --footer-html http://localhost:8080/digitaldocs/footerView.htm?projectId=31609 --margin-top 27mm --margin-bottom 20mm --footer-spacing 3 --header-spacing 5 http://localhost:8080/digitaldocs/printView.htm?documentId=10 -

As Stepan wrote; the problem is mitigated by adding '-q'... The problem did not occur on OSX so maybe its an wkhtmltopdf problem (it seems like it has had similar bugs before which were supposed to be fixed).

BR,
Kalle

@emax19
Copy link

emax19 commented Oct 3, 2018

I have the same problem, but -q flags hide only wkhtmltopdf logs(loading page), libEGL and QPainter logs don't disappear.

private byte[] generatePdf(String html) {
        WrapperConfig wc = getXvfbConfig();

        Pdf pdf = new Pdf(wc);
        pdf.addPageFromString(html);
        try {
            return pdf.getPDF();
        } catch (IOException | InterruptedException e) {
            throw new ServiceException("cant convert html to pdf by wchtmltopdf", e);
        }
    }

    private WrapperConfig getXvfbConfig() {
        XvfbConfig xc = new XvfbConfig();
        xc.addParams(new Param("--auto-servernum"), new Param("--server-num=1"));
        WrapperConfig wc = new WrapperConfig();
        wc.setXvfbConfig(xc);
        return wc;
    }

Created PDF:

libEGL warning: DRI2: failed to open swrast (search paths /usr/lib/x86_64-linux-gnu/dri:${ORIGIN}/dri:/usr/lib/dri)
libEGL warning: DRI2: failed to open swrast (search paths /usr/lib/x86_64-linux-gnu/dri:${ORIGIN}/dri:/usr/lib/dri)
Loading page (1/2)
[> ] 0%
[======> ] 10%
[=======> ] 12%
[========> ] 14%
[==========> ] 17%
[===========> ] 19%
[============> ] 21%
[=============> ] 23%
[===============> ] 25%
[================> ] 27%
[==================> ] 30%
[===================> ] 32%
[====================> ] 34%
[=====================> ] 36%
[======================> ] 38%
[========================> ] 41%
[=========================> ] 43%
[===========================> ] 45%
[============================> ] 47%
[=============================> ] 49%
[============================================================] 100%
Printing pages (2/2)
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::translate: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setWorldTransform: Painter not active
QPainter::setFont: Painter not active
QPainter::setOpacity: Painter not active
QPainter::setBrush: Painter not active
QPainter::brush: Painter not active
QPainter::fillPath: Painter not active
QPainter::end: Painter not active, aborted
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::translate: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setWorldTransform: Painter not active
QPainter::setFont: Painter not active
QPainter::setOpacity: Painter not active
QPainter::save: Painter not active
QPainter::hasClipping: Painter not active
QPainter::window: Painter not active
QPainter::worldTransform: Painter not active
QPainter::setClipPath: Painter not active
QPainter::worldTransform: Painter not active
QPainter::pen: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::drawRects: Painter not active
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::pen: Painter not active
QPainter::setPen: Painter not active
QPainter::restore: Unbalanced save/restore
QPainter::end: Painter not active, aborted
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::translate: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setWorldTransform: Painter not active
QPainter::setFont: Painter not active
QPainter::setOpacity: Painter not active
QPainter::save: Painter not active
QPainter::hasClipping: Painter not active
QPainter::window: Painter not active
QPainter::worldTransform: Painter not active
QPainter::setClipPath: Painter not active
QPainter::worldTransform: Painter not active
QPainter::pen: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::drawRects: Painter not active
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::pen: Painter not active
QPainter::setPen: Painter not active
QPainter::restore: Unbalanced save/restore
QPainter::end: Painter not active, aborted
%PDF-1.4 . . .

@rrsdev
Copy link

rrsdev commented Mar 13, 2019

I ended up creating a new class that I named Pdf2 and copied the code from the original Pdf class. I added a property for "pdfFileOut" and then in the saveAs set that property. In the build command I then check and if that property is set send it instead of the '-' to the wkhtmltopdf command; this saves the file directly and by-passes the stdout issues. I initially looked into simply subclassing Pdf but some of the key methods are private so it didn't make that much sense to do that - if those methods were protected there would be more flexibility (alternatively a pluggable get command class).

@jhonnymertz
Copy link
Owner

@rrsdev nice that you were able to solve and posted the solution you've adopted. Thanks! I'll try to fix it in future releases and improve the code to be more flexible. Anyway, pull requests are welcomed =)

@jhonnymertz
Copy link
Owner

A pull request #53, created by @rrsdev (thanks!), was merged and now we have a method saveAsDirect(String path), which executes wkhtmltopdf passing the path directly as output for wkhtmltopdf, instead of the standard input -. This saves the results directly to the specified file path.

Please, check it out in the release 1.0.10-RELEASE. I've also created a known issues section in the README.md with an explanation regarding this. Maybe this fix does not solve the problem for everyone, because it demands the file to be saved into the file system, but it is a good start.

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

No branches or pull requests

6 participants