question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server hangs on PDF generation

See original GitHub issue

Trying to generate a PDF, this call never returns:

GET /api/method/frappe.utils.print_format.download_pdf?doctype=User&name=Administrator&format=Standard&no_letterhead=0&_lang=en

This sequence of commands, executed by the webserver, hangs it up:

import pdfkit

html = """
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta content="width=device-width, initial-scale=1.0" name="viewport" />
    <title>Administrator</title>
    <meta content="frappe" name="generator" />
    <link href="http://localhost:8000/assets/css/printview.css" rel="stylesheet" type="text/css" />
    <style>
        @media screen {
            .print-format-gutter {
                background-color: #d1d8dd;
                padding: 30px 0px;
            }

            .print-format {
                background-color: white;
                box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.5);
                max-width: 8.3in;
                min-height: 11.69in;
                padding: 0.75in;
                margin: auto;
            }

            .print-format.landscape {
                max-width: 11.69in;
                padding: 0.2in;
            }

            .page-break {
                padding: 30px 0px;
                border-bottom: 1px dashed #888;
            }

            .page-break:first-child {
                padding-top: 0px;
            }

            .page-break:last-child {
                border-bottom: 0px;
            }

            /* mozilla hack for images in table */
            body:last-child .print-format td img {
                width: 100% !important;
            }

            @media(max-width: 767px) {
                .print-format {
                    padding: 0.2in;
                }
            }
        }

        @media print {
            .print-format p {
                margin-left: 1px;
                margin-right: 1px;
            }
        }

        .disabled-check {
            color: #eee;
        }

        .data-field {
            margin-top: 5px;
            margin-bottom: 5px;
        }

        .data-field .value {
            word-wrap: break-word;
        }

        .important .value {
            font-size: 120%;
            font-weight: bold;
        }

        .important label {
            line-height: 1.8;
            margin: 0px;
        }

        .table {
            margin: 20px 0px;
        }

        .square-image {
            width: 100%;
            height: 0;
            padding: 50% 0;
            background-size: contain;
            /*background-size: cover;*/
            background-repeat: no-repeat !important;
            background-position: center center;
            border-radius: 4px;
        }

        .print-item-image {
            object-fit: contain;
        }

        .pdf-variables,
        .pdf-variable,
        .visible-pdf {
            display: none !important;
        }

        .print-format {
            font-size: 9pt;
            font-family: "Helvetica Neue", Helvetica, Arial, "Open Sans", sans-serif;
            -webkit-print-color-adjust: exact;
        }

        .page-break {
            page-break-after: always;
        }

        .print-heading {
            border-bottom: 1px solid #aaa;
            margin-bottom: 10px;
        }

        .print-heading h2 {
            margin: 0px;
        }

        .print-heading h4 {
            margin-top: 5px;
        }

        table.no-border,
        table.no-border td {
            border: 0px;
        }

        .print-format label {
            /* wkhtmltopdf breaks label into multiple lines when it is inline-block */
            display: block;
        }

        .print-format img {
            max-width: 100%;
        }

        .print-format table td>.primary:first-child {
            font-weight: bold;
        }

        .print-format td,
        .print-format th {
            vertical-align: top !important;
            padding: 6px !important;
        }

        .print-format p {
            margin: 3px 0px 3px;
        }

        .print-format table td pre {
            white-space: normal;
            word-break: normal;
        }

        table td div {
            /* needed to avoid partial cutting of text between page break in wkhtmltopdf */
            page-break-inside: avoid !important;
        }

        /* hack for webkit specific browser */
        @media (-webkit-min-device-pixel-ratio:0) {

            thead,
            tfoot {
                display: table-header-group;
            }
        }

        [document-status] {
            margin-bottom: 5mm;
        }

        .signature-img {
            background: #fff;
            border-radius: 3px;
            margin-top: 5px;
            max-height: 150px;
        }

        .print-preview [data-fieldtype="Table"] {
            overflow: auto;
        }

        .print-heading {
            text-align: right;
            text-transform: uppercase;
            color: #666;
            padding-bottom: 20px;
            margin-bottom: 20px;
            border-bottom: 1px solid #d1d8dd;
        }

        .print-heading h2 {
            font-size: 24px;
        }

        .print-format th {
            background-color: #eee !important;
            border-bottom: 0px !important;
        }

        /* modern format: for-test */
    </style>
</head>

<body>
    <div class="print-format-gutter">
        <div class="print-format">
            <div class="page-break">
                <div class="row section-break">
                    <div class="col-xs-12 column-break">
                        <div class="row data-field" data-fieldname="enabled" data-fieldtype="Check">
                            <div class="col-xs-9"><label>Enabled</label></div>
                            <div class="col-xs-3 text-right value"><i class="fa fa-check"></i></div>
                        </div>
                    </div>
                </div>
                <div class="row section-break">
                    <div class="col-xs-6 column-break">
                        <div class="row data-field" data-fieldname="email" data-fieldtype="Data">
                            <div class="col-xs-5"><label>Email</label></div>
                            <div class="col-xs-7 value">admin@example.com</div>
                        </div>
                        <div class="row data-field" data-fieldname="first_name" data-fieldtype="Data">
                            <div class="col-xs-5"><label>First Name</label></div>
                            <div class="col-xs-7 value">Administrator</div>
                        </div>
                        <div class="row data-field" data-fieldname="full_name" data-fieldtype="Data">
                            <div class="col-xs-5"><label>Full Name</label></div>
                            <div class="col-xs-7 value">Administrator</div>
                        </div>
                        <div class="row important data-field" data-fieldname="send_welcome_email"
                            data-fieldtype="Check">
                            <div class="col-xs-9"><label>Send Welcome Email</label></div>
                            <div class="col-xs-3 text-right value"><i class="fa fa-check"></i></div>
                        </div>
                        <div class="row data-field" data-fieldname="unsubscribed" data-fieldtype="Check">
                            <div class="col-xs-9"></div>
                            <div class="col-xs-3 text-right value"><i
                                    class="{{undefined value printed: the inline if-expression on line 127 in 'templates/print_formats/standard_macros.html' evaluated to false and no else section was defined.}}"></i>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-6 column-break">
                        <div class="row data-field" data-fieldname="username" data-fieldtype="Data">
                            <div class="col-xs-5"><label>Username</label></div>
                            <div class="col-xs-7 value">administrator</div>
                        </div>
                    </div>
                </div>
                <div class="row section-break">
                    <div class="col-xs-6 column-break"> </div>
                    <div class="col-xs-6 column-break">
                        <div class="row data-field" data-fieldname="mute_sounds" data-fieldtype="Check">
                            <div class="col-xs-9"></div>
                            <div class="col-xs-3 text-right value"><i
                                    class="{{undefined value printed: the inline if-expression on line 127 in 'templates/print_formats/standard_macros.html' evaluated to false and no else section was defined.}}"></i>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row section-break">
                    <div class="col-xs-12 column-break">
                        <div class="row data-field" data-fieldname="logout_all_sessions" data-fieldtype="Check">
                            <div class="col-xs-9"></div>
                            <div class="col-xs-3 text-right value"><i
                                    class="{{undefined value printed: the inline if-expression on line 127 in 'templates/print_formats/standard_macros.html' evaluated to false and no else section was defined.}}"></i>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row section-break">
                    <div class="col-xs-12 column-break">
                        <div class="row data-field" data-fieldname="document_follow_notify" data-fieldtype="Check">
                            <div class="col-xs-9"></div>
                            <div class="col-xs-3 text-right value"><i
                                    class="{{undefined value printed: the inline if-expression on line 127 in 'templates/print_formats/standard_macros.html' evaluated to false and no else section was defined.}}"></i>
                            </div>
                        </div>
                        <div class="row data-field" data-fieldname="document_follow_frequency" data-fieldtype="Select">
                            <div class="col-xs-5"><label>Frequency</label></div>
                            <div class="col-xs-7 value">Daily</div>
                        </div>
                    </div>
                </div>
                <div class="row section-break">
                    <div class="col-xs-12 column-break">
                        <div class="row data-field" data-fieldname="thread_notify" data-fieldtype="Check">
                            <div class="col-xs-9"></div>
                            <div class="col-xs-3 text-right value"><i
                                    class="{{undefined value printed: the inline if-expression on line 127 in 'templates/print_formats/standard_macros.html' evaluated to false and no else section was defined.}}"></i>
                            </div>
                        </div>
                        <div class="row data-field" data-fieldname="send_me_a_copy" data-fieldtype="Check">
                            <div class="col-xs-9"></div>
                            <div class="col-xs-3 text-right value"><i
                                    class="{{undefined value printed: the inline if-expression on line 127 in 'templates/print_formats/standard_macros.html' evaluated to false and no else section was defined.}}"></i>
                            </div>
                        </div>
                        <div class="row data-field" data-fieldname="allowed_in_mentions" data-fieldtype="Check">
                            <div class="col-xs-9"><label>Allowed In Mentions</label></div>
                            <div class="col-xs-3 text-right value"><i class="fa fa-check"></i></div>
                        </div>
                    </div>
                </div>
                <div class="row section-break">
                    <div class="col-xs-6 column-break">
                        <div class="row data-field" data-fieldname="simultaneous_sessions" data-fieldtype="Int">
                            <div class="col-xs-5"><label>Simultaneous Sessions</label></div>
                            <div class="col-xs-7 text-right value">1</div>
                        </div>
                        <div class="row data-field" data-fieldname="user_type" data-fieldtype="Select">
                            <div class="col-xs-5"><label>User Type</label></div>
                            <div class="col-xs-7 value">System User</div>
                        </div>
                        <div class="row data-field" data-fieldname="login_after" data-fieldtype="Int">
                            <div class="col-xs-5"><label>Login After</label></div>
                            <div class="col-xs-7 text-right value">0</div>
                        </div>
                        <div class="row data-field" data-fieldname="login_before" data-fieldtype="Int">
                            <div class="col-xs-5"><label>Login Before</label></div>
                            <div class="col-xs-7 text-right value">0</div>
                        </div>
                        <div class="row data-field" data-fieldname="bypass_restrict_ip_check_if_2fa_enabled"
                            data-fieldtype="Check">
                            <div class="col-xs-9"></div>
                            <div class="col-xs-3 text-right value"><i
                                    class="{{undefined value printed: the inline if-expression on line 127 in 'templates/print_formats/standard_macros.html' evaluated to false and no else section was defined.}}"></i>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-6 column-break">
                        <div class="row data-field" data-fieldname="last_login" data-fieldtype="Read Only">
                            <div class="col-xs-5"><label>Last Login</label></div>
                            <div class="col-xs-7 value">2020-10-15 14:02:42.473169</div>
                        </div>
                        <div class="row data-field" data-fieldname="last_ip" data-fieldtype="Read Only">
                            <div class="col-xs-5"><label>Last IP</label></div>
                            <div class="col-xs-7 value">127.0.0.1</div>
                        </div>
                        <div class="row data-field" data-fieldname="last_active" data-fieldtype="Datetime">
                            <div class="col-xs-5"><label>Last Active</label></div>
                            <div class="col-xs-7 value">15-10-2020 16:10:04</div>
                        </div>
                        <div data-fieldname="last_known_versions" data-fieldtype="Text" style="padding: 10px 0px">
                            <label>Last Known Versions</label>{"frappe":{"title": "Frappe Framework", "description":
                            "Full stack web framework with Python, Javascript, MariaDB, Redis, Node", "branch":
                            "version-12", "version": "12.10.2"}, "erpnext":{"title": "ERPNext", "description": "ERP made
                            simple", "branch": "version-12", "version": "12.12.1"}}</div>
                    </div>
                </div>
                <div class="row section-break">
                    <div class="col-xs-6 column-break"> </div>
                    <div class="col-xs-6 column-break"> </div>
                </div>
            </div>
        </div>
    </div>
    <script>document.addEventListener('DOMContentLoaded', () => { const page_div = document.querySelector('.page-break'); page_div.style.display = 'flex'; page_div.style.flexDirection = 'column'; const footer_html = document.getElementById('footer-html'); footer_html.classList.add('hidden-pdf'); footer_html.classList.remove('visible-pdf'); footer_html.style.order = 1; footer_html.style.marginTop = '20px'; });</script>
</body>

</html>
"""
options = {
    "print-media-type": None,
    "background": None,
    "images": None,
    "quiet": None,
    "encoding": "UTF-8",
    "margin-right": "15mm",
    "margin-left": "15mm",
    "header-html": "/tmp/frappe-pdf-RANDOM_STRING.html",
    "footer-html": "/tmp/frappe-pdf-RANDOM_STRING.html",
    "cookie": [
        [
            "sid",
            "MY_SID_HERE"
        ]
    ],
    "page-size": "A4",
    "disable-javascript": "",
    "disable-local-file-access": "",
    "disable-smart-shrinking": ""
}
filedata = pdfkit.from_string(html, False, options=options or {}) # this call never returns

https://github.com/frappe/frappe/blob/02e7844690b4df93197f6f50f37004b7af50ad32/frappe/utils/pdf.py#L40

After this, the server stops to answer any other requests.

Trying to reproduce the issue with the same inputs from above in bench console gives me this error:

filedata = pdfkit.from_string(html, False, options=options or {})
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
~/frappe-bench-develop/apps/frappe/frappe/commands/utils.py in <module>
----> 1 filedata = pdfkit.from_string(html, False, options=options or {})

~/frappe-bench-develop/env/lib/python3.6/site-packages/pdfkit/api.py in from_string(input, output_path, options, toc, cover, css, configuration, cover_first)
     70                configuration=configuration, cover_first=cover_first)
     71 
---> 72     return r.to_pdf(output_path)
     73 
     74 

~/frappe-bench-develop/env/lib/python3.6/site-packages/pdfkit/pdfkit.py in to_pdf(self, path)
    154 
    155         if 'Error' in stderr:
--> 156             raise IOError('wkhtmltopdf reported an error:\n' + stderr)
    157 
    158         if exit_code != 0:

OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: HostNotFoundError

Wkhtmltopdf works, in priciple:

> bench console
In [1]: import pdfkit

In [2]: filedata = pdfkit.from_string('<h1>Test</h1>', False)
Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done 

Environment

Development setup, bench started via bench start.

> bench --version
5.0.0

> bench version
erpnext 12.12.1
frappe 12.10.2

> wkhtmltopdf --version
wkhtmltopdf 0.12.5 (with patched qt)

> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:        18.04
Codename:       bionic

Seems to be something wrong with my setup. Any hints what it could be?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
adityahasecommented, Oct 16, 2020

@barredterra Also, is it possible that you’re running a single threaded web server? I could replicate this by setting

threaded=False

in https://github.com/frappe/frappe/blob/develop/frappe/app.py#L272

Also, If the PDF generation works after removing

<link type="text/css" rel="stylesheet" href="/assets/css/printview.css">

from printview.html (and maybe other assets), (you’ll get an unstyled print though) then single threaded server is the problem.

0reactions
sagarvoracommented, Oct 16, 2020

Ok, maybe not a minute for rendering. This is the time taken for the initial response in one of the formats (49 pages) on a very high performance production server:

Screenshot from 2020-10-16 17-35-51

Perhaps this could be configurable or should raise an error immediately if single threaded can never work…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Puppeteer-PDF: Node hangs when I try to generate a PDF
I using Puppeteer to generate a PDF file. ... Server 2012 R2) it hangs when it try to generate the PDF and it...
Read more >
Pdf generation freezes while loading - Jaspersoft Community
I have some problems generating a pdf. I need to generate a big table in a subreport, but in some cases this table...
Read more >
FMSE Stuck Saving PDFs - Claris Community - FileMaker Pro
When saving the PDF to the documents folder of the server, on a seemingly random occurrence, the PDF will have a size of...
Read more >
Troubleshoot errors, freezes | Reader | Windows
-- If the problem doesn't recur, then move the icons and shortcuts back to the Startup folder one at a time and restart...
Read more >
PDFCreator hangs sometimes with many files left in ...
I've tried to kick the processing with many methods: delete all files from the spool, restarting the server, none of them helped. Only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found