You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As stated in the documentation the img.convert_to_pdf() function does not fully support SVG to PDF conversion. For example, I've encountered an issue with the <style> tag in SVG files, see this issue. As this limitation is mentioned in the docs, it's not a bug, but you probably still want to have your SVG files rendered correctly.
Therefore, if you face such a limitation, you could draw on other libraries. For me, cairosvg worked great. Install it via pip
pip install cairosvg
Then use cairo to convert your SVG file to PDF bytes and finally open these bytes with fitz. It's as simple as:
defconvert_svg_to_pdf(path) ->bytes:
withopen(path, 'r') asf:
returncairosvg.svg2pdf(bytestring=f.read())
img_path='your/path/to/the/image.svg'pdfbytes=convert_svg_to_pdf(img_path)
# Open image in fitz to read image dimensions (use rect later)img=fitz.open(img_path)
rect=img[0].rect# pic dimensionimg.close()
# Process pdf with fitzpdf=fitz.open('pdf', pdfbytes) # open stream as PDF# Do something with pdf, e.g.page=doc.new_page(width=rect.width, height=rect.height)
page.show_pdf_page(rect, pdf, 0)
doc.save('your/out/path.pdf')
...
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As stated in the documentation the
img.convert_to_pdf()
function does not fully support SVG to PDF conversion. For example, I've encountered an issue with the<style>
tag in SVG files, see this issue. As this limitation is mentioned in the docs, it's not a bug, but you probably still want to have your SVG files rendered correctly.Therefore, if you face such a limitation, you could draw on other libraries. For me,
cairosvg
worked great. Install it via pipThen use cairo to convert your SVG file to PDF bytes and finally open these bytes with fitz. It's as simple as:
Beta Was this translation helpful? Give feedback.
All reactions