Looking for more straightforward way to extract svg #1175
Replies: 3 comments 4 replies
-
I actually like your code 😎 But you probably need no dummy ...
page.set_cropbox(r)
svg = page.get_svg_image()
... |
Beta Was this translation helpful? Give feedback.
-
By the way, I did a little experiment to find which bounding box (like MediaBox) can clip as I wanted. for page in out:
xref = out.page_xref(page.number)
print("page:", page.number)
for key in doc.xref_get_keys(xref):
print(key, "=" , doc.xref_get_key(xref, key)) Then ran the code with the sample.pdf that was attached in my previous comment (containing 3 blue quadrangles)
It's chaotic 🤯 |
Beta Was this translation helpful? Give feedback.
-
Don't know what you have been doing ... 🤔 import fitz
doc = fitz.open("sample.pdf")
page = doc[0]
paths = page.get_drawings()
src = fitz.open()
for i, path in enumerate(paths):
r = path["rect"]
spage = src.new_page(width=r.width, height=r.height)
spage.show_pdf_page(spage.rect, doc, page.number, clip=r)
svg = spage.get_svg_image(text_as_path=False)
out = open("%i-new.svg" % i, "w")
out.write(svg)
out.close()
for page in src:
keys = src.xref_get_keys(page.xref)
print("\nTempfile page", page.number, "\n")
for k in keys:
print(k, src.xref_get_key(page.xref, k)) Result:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to extract drawings from a PDF file and save them as SVG.
Thanks to a previous work found in here, I managed, but the implementation feels a bit roundabout to me.
What my current implementation below does is extract drawings, get their rect size, crop the source pdf to that size, and finally
get_svg_image
; such a long way!I want to find any better way than that. Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions