WeasyPrint icon indicating copy to clipboard operation
WeasyPrint copied to clipboard

SVG Image used as Logo is duplicated in PDF for every page

Open jsf84ksnf opened this issue 2 years ago • 5 comments

Cou can use an svg image as a logo ( @page { @top-right{ background-image: url("/static/logo.svg")}} ) and it works just fine.

But if the document has > 1 pages, the full SVG resource is included for every page in the pdfs, blowing up its size greatly. (100 pages includes the svg 100 times).

I also tried defining the logo as a svg-symbol with an id and then loading the id with svg-use, but that doesnt show up at all.

WeasyPrint Version: 60.1

jsf84ksnf avatar Oct 05 '23 08:10 jsf84ksnf

Hi!

Thanks for the report.

That’s right, when a SVG file is drawn multiple times, it’s included multiple times. That’s not the case for raster images, because a cache is used to avoid this, and we reference the PDF’s XObject each time the image is drawn. We should be able (hopefully) to store the drawing once in the PDF and reference it each time it’s required. But I don’t know yet which PDF structure to use for that.

liZe avatar Oct 06 '23 14:10 liZe

Hello @liZe ,

I am experiencing the same issue. I generate documents with icons using an SVG spritesheet. However, it seems that the entire SVG is loaded each time an icon is retrieved:

background-image: url("./spritesheet.svg");
background-repeat: no-repeat;
background-size: 20px 20px;

From reading this discussion thread, I understand that the SVG is reloaded every time, which can result in a PDF document size of 4 MB for 14 pages. If I remove the icons entirely, the file size can decrease to 770 KB.

Is there a planned update for WeasyPrint to address this issue?

Thank you in advance

m-cisse-easy-live avatar May 17 '24 09:05 m-cisse-easy-live

Is there a planned update for WeasyPrint to address this issue?

There’s nothing planned for this issue.

If you (or anyone) is interested in solving this issue, the first step is to find in the PDF specification a way to store the drawing once and reference it multiple times.

liZe avatar May 17 '24 09:05 liZe