Editorial Workflows

PDF (PDF This Page)

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Convert the current markdown file to html and open the html in the iOS app 'PDF This Page', whence you can convert it to a PDF. Has a CSS for printing, with classes .floatright (usually for an image) and .pagebreak.

Requires the app 'PDF This Page' (or 'Red: Web to PDF converter').

The Python script to serve the html is from the workflow 'Render to PDF' by @myvanity.

Shared by: Peter Hopcroft (peterh)

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
About this workflow ?
Source Code
#coding: utf-8

Convert the current markdown file to html and open the html in the iOS app 'PDF This Page', whence you can convert it to a PDF. Has a CSS for printing, with classes .floatright (usually for an image) and .pagebreak.

Requires the app 'PDF This Page' (or 'Red: Web to PDF converter').

The Python script to serve the html is from the workflow 'Render to PDF' by @myvanity.

I tried several iOS apps to convert to PDF and, 'PDF This Page' is the best I found: it lets you set the page size to A4 or US, converts to a PDF with one tap, looks nice and does not have any obvious bugs. It is a paid app, a couple of dollars. There is a similar app called 'Red: Web to PDF converter' from the same developer that works much the same; it is free for the first 20 conversions. To use Red, change the last action in the workflow where indicated.

All iOS apps to convert html to PDF (that I have seen) have a bug with the CSS rule page-break-after; and many have a bug with page-break-before.


Using the CSS in this workflow:
You can set left and right margins in the CSS. You can't set top or bottom margins or a header or footer. Set the page size first in PDF This Page.

Images: 1 px is 0.26 mm (the dpi doesn't matter):
- An image 600 px wide is about 160 mm wide, the width of the print area of typical PDF. If an image is wider than about 600 px, then the CSS max-width:100% rule will reduce the image to fit the print area.
- An image about 20 px high is about 5 mm high, suitable for inserting inline with text in a block.

Class .floatright: for one block element, usually an image. You can't apply it to a <div> element around several Markdown elements, because the enclosed markdown is not converted to html. To use:
![](img link){: .floatright }

Class .pagebreak: inserts a page break before the element. To use:
This is a block element.
{: .pagebreak }

A setext style header {: .pagebreak }

### A hash style header {: .pagebreak }

[blank line]
<div class=pagebreak></div>
[blank line]

Document Text ?
Folded Text
  • Include
  • Replace with:
Convert markdown to html, with tables & attribute lists ?
Source Code
#coding: utf-8
import markdown
import workflow

text = workflow.get_input()
text = markdown.markdown (text, extensions=['tables', 'attr_list'] )
Set Variable ?
Variable Name
Get Current File Name ?
Include Folder
Include Extension
Set Variable ?
Variable Name
Generate the CSS ?
/* Basic CSS, for a PDF, Peter Hopcroft, Dec 2013 */ body {font-size: 13pt; font-family: 'palatino', serif; /* PDF This Page adds default left and right margins to these */ margin-left: 12mm; margin-right: 12mm; /* text-align:justify;*/ line-height: 130%; } p, h1, h2, h3, h4, h5, h6, pre, blockquote, ul, ol, li, table, th, tr /* if you change margins, change table, list and hr margins */ {margin-top: 2.5mm; margin-bottom: 0mm; margin-left:0mm; vertical-align: baseline;} h1, h2, h3 {page-break-after: avoid; } /* doesn't work */ h1 {font-size: 16pt; font-weight: bold; margin-top: 14mm; margin-bottom: 4mm} h2 {font-size: 13pt; font-weight: bold; margin-top: 8mm; margin-bottom: 0mm;} h3, h4, h5, h6 {font-size: 13pt; font-weight: normal; margin-top: 8mm; margin-bottom: 0mm; font-style: italic;} em {font-style: italic;} strong {font-weight: bold;} blockquote {margin-left: 5.5mm; font-size: 12pt;} /* match list indent */ pre {font-size: 11pt; padding-left: 5.5mm; /* match list indent */ font-family: 'courier', monospace;} img {max-width: 100%;} hr {margin-top: 3mm; margin-bottom:3.8mm; width: 30%; color: grey; */height: thin;*/} ul {list-style-type: square; margin-left: 5mm;} ol {list-style-type: decimal; margin-left: 5.5mm;} ul,ol {padding-left: 0mm;} table {border-top: thin solid grey; border-bottom: thin solid grey; border-collapse: collapse; margin-top: 3.3mm; margin-bottom: 4mm;} /* page-break-inside: avoid does't work */ th,td {padding-right: 4mm; padding-bottom: 2.5mm; text-align: left;} th {padding-top: 3.3mm; font-weight: bold;} td {padding-top: 0mm;} /* classes */ .floatright {float:right; width:50%; margin: 0mm; padding-bottom:3mm;padding-left:4mm;} .pagebreak {page-break-before: always;}
Set Variable ?
Variable Name
Create the full html ?
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Title</title> <style type="text/css"> CSS </style> </head> <body> Body </body> </html>
Serve HTML to the app PDF This Page ?
Source Code
#coding: utf-8
# Sets up a temporary local web server to serve the html,
# then passes the URL to the app PDF This Page via its URL scheme.
# Based on the script in the workflow 'Render to PDF', by: @myvanity:
# http://editorial-app.appspot.com/workflow/5660638047109120/vHXf1o0Is1o

from BaseHTTPServer import BaseHTTPRequestHandler
from SocketServer import TCPServer
import clipboard
import threading
import time
import webbrowser
import workflow

CHARSET = 'utf-8'

class HTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_header('Content-type', self.server.content_type)

    def log_request(self, *args):
        pass    # Prevent console from appearing

class HTTPServer(TCPServer):
    def __init__(self, content, content_type):
        self.content = content
        self.content_type = content_type
        TCPServer.__init__(self, ('', 0), HTTPRequestHandler)

httpd = HTTPServer(workflow.get_input().encode(CHARSET),
    'text/html; charset=' + CHARSET)

thread = threading.Thread(target=httpd.handle_request)
thread.daemon = True

url = '%s:%i' % httpd.server_address
webbrowser.open('pdfthispage://open?url=http://' + url)
# To use with the app Red, replace pdfthispage with red

# Raises when scheme not registered, except in Editorial
if thread.isAlive():
    raise KeyboardInterrupt('Offer timed out')