0

I am trying to generate pdf of the order summary in my ecommerce website but receiving the following error.

OSError: wkhtmltopdf reported an error:
Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
[=========>                                                  ] 16%
[============>                                               ] 20%
[================>                                           ] 27%
[===================>                                        ] 32%
[=====================>                                      ] 36%
[========================>                                   ] 40%
[=========================>                                  ] 42%
[===================================>                        ] 59%
[=====================================>                      ] 62%
[========================================>                   ] 68%
[===========================================>                ] 72%
[============================================>               ] 74%
[==============================================>             ] 77%
[===============================================>            ] 79%
[=================================================>          ] 82%
[==================================================>         ] 84%
[====================================================>       ] 87%
[=====================================================>      ] 89%
[============================================================] 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 2
[============================================================] Page 2 of 2
Done                                                                      
Exit with code 1 due to network error: RemoteHostClosedError

I am using flask as the webframework and here's the code:

    rendered = render_template("order-summary.html",
                               total_cost=total_cost,
                               name=name,
                               price=price)
    config = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
    pdf = pdfkit.from_string(rendered, False, configuration=config)
    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'inline; filename=output.pdf'
    return response

I searched over the internet but didn't got anything relevant that solves the problem.

Aditya Naitan
  • 162
  • 3
  • 11
  • 1
    See [this answer](https://stackoverflow.com/a/41700417/2052575) which suggests wkhtmltopdf is having trouble loading external URLs. In the context of your code, as you're using `pdfkit.from_string` this might suggest that there are URL's within the rendered `order-summary.html` which the server is having trouble loading (CSS/script imports perhaps). I'd try loading an HTML template which doesn't load external assets to rule that out. – v25 Jan 03 '21 at 13:32

1 Answers1

0

Pass the external *.css file path used inside order-summary.html as agrument in from_string method. df = pdfkit.from_string(rendered, False, configuration=config, css=['./file1.css','./file2.css'])