Skip to content

Instantly share code, notes, and snippets.

@tmr232
Last active October 21, 2023 11:00
Show Gist options
  • Save tmr232/9d7f32a2f4316c809947187ced2bff53 to your computer and use it in GitHub Desktop.
Save tmr232/9d7f32a2f4316c809947187ced2bff53 to your computer and use it in GitHub Desktop.
Edge enables all PDF layers for printing
"""
Create a PDF with multiple layers to demonstrate issues with MS Edge printing.
See https://github.com/pymupdf/PyMuPDF-Utilities/blob/master/jupyter-notebooks/optional-content.ipynb for reference.
"""
import fitz # PyMuPDF
def main():
doc = fitz.open()
page = doc.new_page(height=400, width=500)
text_display = "This is the text I want to print."
text_print = "But this is the text that gets printed."
oc_display = doc.add_ocg("display", on=True)
oc_print = doc.add_ocg("print", on=False)
page.insert_textbox(
rect,
text_display,
fontsize=12,
oc=oc_display,
)
rect = fitz.Rect(50, 50, 400, 300)
page.draw_rect(
rect + (-5, -5, 5, 5),
fill=fitz.pdfcolor["white"],
color=fitz.pdfcolor["white"],
oc=oc_print,
)
page.insert_textbox(
rect,
text_print,
fontsize=12,
oc=oc_print,
)
doc.save("test.pdf")
if __name__ == "__main__":
main()
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment