Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Convert Markdown to PDF with Pythonista (offline, no other apps!)
-
Hi all,
I bought Pythonista yesterday, having been made aware of its presence by a talk at the EuroPython conference. I've been playing around with it for hours already... This finally turns my iPad2 into an actually useful device (besides surfing and usage as sheet music display)!
I had been looking for a way to write text notes on the iPad and convert them into PDF files without the need for a commercial app or a network connection (I often write lead sheets for my band during train rides), but all the solutions I found either required buying another app, just for the purpose of PDF export or to use a web service, which doesn't really work well on-the-go.
Anyway, to cut a long story short: with Pythonista and some external pure-Python packages I was able to write my own solution in no time.
Here it is: MarkdownPDF.py
The script contains some documentation in its docstring (in Markdown format, naturally). To use it, you need to install the required Python packages. I have created another small script
to download a Zip archive I created, which bundles all the required packages, and extracts them to the 'site-packages' sub-directory:Download script: download_md2pdf.py
Put both scripts in your pythonista documents folder, run the
download_md2pdf.py
script and check the console if everything went ok and also check yoursite-packages
directory for the new packages. You can delete thedownload_md2pdf.py
script afterwards.Then run the
MarkdownPDF.py
script and follow the prompts (first startup may take some time). You will be able to either view the documentation, convert Markdown from the clipboard or from a URL. After the conversion, you will be asked whether you want to save the PDF output to a file, open it in another app, or view it withconsole.quickview()
(from where you can also open it in another app).Have fun with it and if you have suggestions, comments, bug reports etc., let me know!
-
Thank you! I've been exploring different methods of pdf creation for a work application. I never considered markdown to pdf.
-
Hi,
any chance that this is possible in Editorial as well? I'm currently trying to "port" this to Editorial, so far without success. The script downloads the required packages but the site-packages folder doesn't get created. Maybe this is not possible at all, but if someone knows a solution, that would be great!
Ben
-
I don't have or know Editorial, so I don't know how installation of third-party Python packages works for this app.
The
download_md2pdf.py
script was a bit naive in that it just looked for asite-packages
folder in the current working directory, and if it doesn't find one, created it. Maybe scripts in Editorial are run in a different directory than in Pythonista, i.e. one, that doesn't allow creating subdirectories?Anyway, I just updated the gist to use
expanduser('~/Documents/site-packages')
instead.If that doesn't work, you can always download the markdown2pdf.zip zip file manually, e.g. with my github_download script (works for normal URLs as well) and unzip it to your chosen location with this code:
import zipfile with zipfile.ZipFile('markdown2pdf.zip') as z: z.extractall('some_existing_directory')
You can also download
markdown2pdf.zip
to your desktop computer, unzip it, copy themarkdown2pdf.py
module to your iOS device via other means (email, dropbox, etc.) and try to install the other dependencies (PyPDF2, ReportLab, xhtml2pdf) via other means. For example you could create Python wheels for each of these packages on your desktop computer, copy them to your iOS device and extract them there (they are normal Zip archives with a.whl
extension). -
Editorial is a scriptable text editor from the creator of pythonista, it's awesome! I got the pdf script to work by setting the module search path first (sys.path), I will upload the whole thing as a one-click workflow later.
One minor problem is, that the underlying module doesn't seem to include local images in the pdf. Images referenced from the web are no problem. I don't know if this can be solved but I think this is as good as it gets.
Also, if I use @page in my style, a black border appears around my content. This seems to be a known problem with an older version of xhtml2pdf and/or Reportlab, I don't know exactly.
-
Also, if I use @page in my style, a black border appears around my content. This seems to be a known problem with an older version of xhtml2pdf and/or Reportlab, I don't know exactly.
The version of xmhtl2pdf I bundled was the latest state of its git repo. For ReportLab I bundled version 3.1.8.
-
Well I got the borders to disappear after fixing a file in reportlab, as described here: https://bitbucket.org/rptlab/reportlab/issue/29/platypus-frames-showboundaryvalue-no
It seems that the fix isn't yet officially released, I don't know.
Now I just need a proper styling and then maybe a simple GUI for downloading xhtml2pdf and reportlab, then I will release it as a single workflow for editorial. -
Now I just need a proper styling and then maybe a simple GUI for downloading xhtml2pdf and reportlab
Don't forget the PyPDF2 dependency.
-
Know this is old, but wondering about being able to get correct formatting for tables.