FPDF2 issue importing svg with text

76 Views Asked by At

I have a problem to load the SVG (created by QBill) to the FPDF2 library. The shapes are adopted without any problems, but the text is missing everywhere from the SVG. Here is the code:

import fpdf
from qrbill import QRBill
import tempfile
import os

# create a QRBill object
my_bill = QRBill(
    account='CH5800791123000889012',
    creditor={'name': 'Jane', 'pcode': '1000', 'city': 'Lausanne', 'country': 'CH'},
    amount='22.45',
    currency='CHF'
)

# save the svg to a temporary file
with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.svg') as temp_svg_file:
    my_bill.as_svg(temp_svg_file)
    temp_svg_file_path = temp_svg_file.name

# create a fpdf object
pdf = fpdf.FPDF(unit="mm", format=(210, 297))
pdf.add_page()

# load the svg from the temporary file
svg = fpdf.svg.SVGObject.from_file(temp_svg_file_path)

# transform the svg to the page viewport
width, height, paths = svg.transform_to_page_viewport(pdf, align_viewbox=False)
paths.transform = paths.transform @ fpdf.drawing.Transform.translation(-width, -height).rotate_d(0).translate(pdf.w, pdf.h)

pdf.draw_path(paths)
pdf.output("./Out/SVG_Test_qrbill.pdf")

os.remove(temp_svg_file_path)

# Unfortunately, with its SVG function, FPDF only takes over the vectors, not the text!

I suspect that the text must also be converted into paths, or there is a better (faster) way to load the SVG into the FPDF2. The question of why I am not converting it into PNG/JPG, it is elementary that the shapes and texts in the PDF are not rastered (print quality reasons, and too large PDF, because we have 100,000 pages that are to be printed), so we want to avoid an conversion by Cairo and Co.

Does anyone have an idea how I can solve the SVG text problem in FPDF2? Or is there a better solution without reducing the performance?

Hope there are graphic/PDF specialists to help me out.

1

There are 1 best solutions below

0
On

If you try using the latest fpdf2 release, 2.7.7, you should see this output when running your code:

Ignoring unsupported SVG tag: <{http://www.w3.org/2000/svg}text> (contributions are welcome to add support for it)

This is a current know limitation of fpdf2: https://github.com/py-pdf/fpdf2/issues/537, <text> tags in SVG are not supported for now.