PDF to image with optional content #2136
-
Hello! I'm looking for a way of converting a PDF to either PNG or SVG one layer at a time. I know that OCGs are tricky business in PDFs, but I was hoping that PyMuPDF might have this functionality already. I have tried the following, for a document for xref, ocg_dict in doc.get_ocgs().items():
doc.set_layer(-1, basestate="OFF", on=[xref])
for page in doc.pages():
dl = page.get_displaylist()
pix = dl.get_pixmap()
pix.save(f"page{page.number}-{ocg_dict['name']}.png") This gave me 20 identical images per page, with each showing the content of all layers. I also tried modifying the visibility with I'm guessing layer visibility isn't taken into account with the underlying |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Can you let me have that PDF? |
Beta Was this translation helpful? Give feedback.
-
@cfcurtis - just wondered why you call your solution "ugly". Maybe you wanted PDF results that didn't have images as pages? But instead pages in original PDF format? |
Beta Was this translation helpful? Give feedback.
-
Hi, how are you? |
Beta Was this translation helpful? Give feedback.
Can you let me have that PDF?
You sure that the OCGs really imply different views?