Pisa PDF converter is very slow with large tables

I use Pisa to convert HTML to PDF (in a Django project). This is very slow when processing tables that span multiple pages:

a 200 row table takes up to 150 seconds to convert, and takes 15 seconds if I split it into smaller tables.

Are there any tips or tricks for creating HTML tables for processing by Pisa?

+8
django html-table pdf-generation pisa xhtml2pdf
source share
1 answer

I had the same problem. The document was just the front page and a huge table. PDF rendering time increased exponentially with the size of my content table.

I made a checklist of things to check what could be the problem

I made a simple timing in my PDF rendering function (since it could be rendering HTML, passing it to StringIO or creating an HTTP response), and noticed that calling pisa.pisaDocument took 60 seconds to return. I made a checklist of things that could be a problem, and each one worked on them. The checklist included images, CSS, layout complexity, and frames.

Images barely affected rendering time (I only had one per page, so YMMV). Nor were Frames.

The complexity of the markup was the main problem of my template. Obviously, pisa will make several columns in a table very, very slowly.

There was too much time in the table for rendering, but I noticed that if I split the table into smaller tables, the rendering time did not increase exponentially, and the time taken to do everything was halved. I used the code below in my Django template:

{% if forloop.counter|divisibleby:20 %}</table><table>{% endif %} 

edit: This fix doesn’t work very well with repeating table headers, so if you do repeat="1" , you need to know exactly how many rows will correspond to each page.

Also, I had this selector monster in my CSS:

  html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video{ ... } 

Changing it to * {...} , rendering a bit faster. This is inconsistent, as browsers will not display your page as quickly when you use the * selector than when you use the aforementioned monster.

In addition, for some reason, merging two <style> tags into one tag also reduced rendering time.

+7
source share

All Articles