Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rich output to PDF
#1
Is it possible to create an PDF based on Rich output?

Or is there another solution/package in Python to create a PDF with RTF?
Reply
#2
For this method you need the Python modules fitz and striprtf.

The module pypandoc could do this in less lines, but it doesn't work for me. .rtf is not included in the file types.

PDFs are complicated things. You need to tell fitz various things.

Check out the docs for fitz (PyMuPDF) here.

import fitz from striprtf.striprtf import rtf_to_text print(fitz.__doc__) rtf = '/home/pedro/Documents/ancient_hero.rtf' # an rtf encoded file savepath = '/home/pedro/Documents/ancient_hero.pdf' # get the text from the rtf encoded file using stripf module with open(rtf) as f: pdfdata = f.read() pdftext = rtf_to_text(pdfdata) # set output page size MEDIABOX = fitz.paper_rect("A4") # output page format: A4 # set the margins 72 points = 1" WHERE = MEDIABOX + (36, 36, -36, -36) # leave borders of 0.5 inches story = fitz.Story() # create an empty story body = story.body # access the body of its DOM with body.add_paragraph() as para: # store desired content para.set_font("sans-serif").set_color("black").add_text(pdftext) writer = fitz.DocumentWriter(savepath) more = 1 while more: device = writer.begin_page(MEDIABOX) more, _ = story.place(WHERE) story.draw(device) writer.end_page() writer.close() 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can rich.logging output to file? pyfoo 1 4,259 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  Help with TypeWriter Effect in Python Rich Extra 0 2,419 May-23-2022, 09:44 PM
Last Post: Extra
  Help adding prompt text in a Layout using Rich TUI Extra 2 3,363 May-23-2022, 07:15 PM
Last Post: Extra

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.