DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. An Intro to rst2pdf – Changing Restructured Text into PDFs with Python

An Intro to rst2pdf – Changing Restructured Text into PDFs with Python

Mike Driscoll user avatar by
Mike Driscoll
·
Jun. 20, 12 · Interview
Like (0)
Save
Tweet
Share
6.25K Views

Join the DZone community and get the full member experience.

Join For Free

There are several cool ways to create PDFs with Python. In this article we will be focusing on a cool little tool called rst2pdf, which takes a text file that contains Restructured Text and converts it to a PDF. The rst2pdf package requires Reportlab to function. This won’t be a tutorial on Restructured Text, although we’ll have to discuss it to some degree just to understand what’s going on.

Getting Started

 

First off we’ll need to create a document with the required markup. Let’s do that first. Here’s some simple restructured text with a couple of directives mixed in. We’ll explain everything after you’ve had a chance to read the code:

.. header::
Python Rules! - page ###Page###

=====
Title
=====

This is some blah blah blah

.. raw:: pdf

PageBreak oneColumn

New section
===========

yada yada yada

import urllib
import urllib2
import webbrowser

url = "http://duckduckgo.com/html"
data = urllib.urlencode({'q': 'Python'})
results = urllib2.urlopen(url, data)
with open("results.html", "w") as f:
f.write(results.read())

webbrowser.open("results.html")

 

 

The first couple lines define the header that will be on every page. In this case, we’re going to have “Python Rules!” printed at the top of every page along with a page number. There are several other special hash-mark insert directives available. You should check out the official documentation for more information on those. Then we have a Title. Note that it is preceded and followed by a bunch of equal signs that are the same length as the text. This tells us that this text will be styled and centered. The following line is just a lame sentence for demonstration purposes. Next up is another special directive which tells rst2pdf to insert a page break. The second page contains a section header, a lame sentence and a code example that’s color-coded.

To generate the PDF, you’ll need to do something like this on the command line:

rst2pdf test.rst -o out.pdf

You can also run rst2pdf against a config file to control some of the special PDF directives, like header and footer, etc. The information about how to make the config file get read is a little confusing though. It sounds like you have to place the file in a specific location: /etc/rst2pdf.conf and ~/.rst2pdf/config. There’s also a –config flag you can pass, but I’ve found various reports online that that doesn’t work, so your mileage may vary. There’s a sample config file in the project’s repo that you’ll find instructive.

Wrapping Up

 

I was hoping that rst2pdf would allow an easy way to specify absolute positions and create lines and boxes so I could replace an XSL / XML project I was working on with something much more simple. Alas, rst2pdf just doesn’t support the lines and boxes that reportlab itself does at the time of this writing. However if you need something easy to use to create your documents with and you already know restructured text, I think this is a very good way to go. You can also take your restructured text skills and use them with the Sphinx documentation project.

Python (language)

Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices 101: Transactional Outbox and Inbox
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Low-Code Development: The Future of Software Development
  • A Gentle Introduction to Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: