-
Notifications
You must be signed in to change notification settings - Fork 94
There are two basic things the backend server API provides: printing, and previewing. Let's start with the previewing.
To preview, you make a simple request:
GET http://printer.gofreerange.com/preview?url=<url>
You can also POST
with the url
parameter.
If you omit the url
parameter, the server will use the HTTP_REFERER
from the request. This can sometimes be convenient — for example, you could add a link to http://printer.gofreerange.com/preview
from the page that you wish to print, as long as that page is publicly accessible.
You can also POST
with HTML in the content
parameter; this is particularly useful if you can’t make your content available at a publicly-accessible URL.
However you send the request, you’ll be redirected to a page where, in a few seconds, you’ll see an image that approximates what will be sent to the printer (although there is still some further processing to make it monochrome, for example).
If you make the request programmatically, you’ll probably be returned a JSON object which contains the URL for the preview.
To print a page, you need to make a very similar request:
GET http://printer.gofreerange.com/print/<printer_id>?url=<url>
The server will process the page at that URL, and in a short while (normally less than a minute) it will be available for the printer with that ID
to download.
The HTTP_REFERER
trick above also works here, as does sending a POST
request, and using the content
parameter.
When rendering (either for preview, or for printing), the server will remove any preview
class that exists on the body
of the page, and then attempt to convert it to a 384-pixel-wide image.
This preview
class can be very useful if you want to be able to design the page in “simulated printout” form, but leave the page unadorned by backgrounds and margins when it is being processed for printing. You can achieve this by hiding any extraneous items and changing margins and backgrounds in the absence of the preview
class.
The server provides a sample stylesheet that will do this very simply for you.
The rendering is performed by phantomjs running on Linux, and so there are a fairly limited number of system fonts available to you. It’s worth bearing this in mind.
Of course, if you know how to install more fonts, you can always run your own copy of the server yourself; my source code is available for anyone to use and improve on.
Alternatively, if you want to improve phantomjs to use web-fonts, then I’d greatly appreciate it!