GithubHelp home page GithubHelp logo

alexhayes / pythonic-pdflib Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 2.0 156 KB

PythonicPdflib is an attempt to create a more "pythonic" API for PDFlib.

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

pythonic-pdflib's Introduction

PythonicPDFlib

PythonicPDFlib is an attempt to create a more "pythonic" API for PDFlib.

Input Attributes

Many methods within PDFlib require "odd" or un-pythonic input attributes for their default values.

PythonicPdflib behaves how you would expect a Python API to behave - that is, sensible defaults and options are a dict, not a string of space separated values.

Color Data Type

Rather than define RBG as a float between 0-1 you can define as an integer between 0-255, as follows:

from pythonic_pdflib import RGBColor
tf = p.add_textflow('example text', options={'fillcolor': RGBColor(71, 76, 148)})

vs.

tf = p.add_textflow(-1, 'example text', 'fillcolor={rgb 0.278431373 0.298039216 0.580392157}')

Note this behaviour needs to be implemented for CMYK, grey and the other color data types. Feel free to fork and submit a pull request.

Context Managers

There are a lot of begin_* methods which each require their associated end_* method to be called once you've finished using them.

PythonicPdflib provides context managers which allow you to use the with statement to manage the relevant calls to begin_*/end_*.

All begin_*/end_* and some open_*/close_* methods have a corresponding context manager simple titled as the last part of the method. For example, instead of using begin_font/end_font simply use font.

For example:

p = PythonicPDFlib({'license': 'my-license'})
with p.document('/path/to/my.pdf'):
    # do stuff

vs.

p = PDFlib()
p.set_option('license=my-license')
p.begin_document('/path/to/my.pdf', '')
# do stuff...
p.end_document('')

Example

width = 595
height = 842

p = PythonicPDFlib({'license': 'my-license'})

with d.document('/path/to/my.pdf'):
	with p.pdi_document('/path/to/my/template.pdf') as doc:
		page_no = 1
	    with p.pdi_page(doc, page_no) as page:
	        with p.page_ext(width, height):
	        	# Adjust the pdf size to match the template.pdf
	            p.fit_pdi_page(page, 0, 0, {'adjustpage': None})

	            textflow_options = {
	            	'fontname': 'Trebuchet_MS', 
                    'fontsize': 25,
                    'encoding': 'unicode',
                    'fillcolor': RGBColor(255, 255, 255),
                    'alignment': 'center'
				}
	            textflow = self.add_textflow("Hello World - %s" % page_no, options=textflow_options)
	        	self.fit_textflow(tf, 0, 743, self.size.width, 770, options={'verticalalign': 'center'})
		
		page_no = 2
	    with p.pdi_page(doc, page_no) as page:
	        with p.page_ext(width, height):
	        	...

Obviously this example is extremely nested and perhaps one would agree even un-pythonic... but it is just supposed to be an illustration of how the API behaves.

Requirements

Installation

pip install git+git://github.com/alexhayes/pythonic-pdflib.git

Testing

nosetests

Version

PythonicPDFlib is currently considered alpha - the API will likely change as it's in early development.

Author

Alex Hayes [email protected]

Thanks

Thanks to roi.com.au for allowing this code to be released open source.

pythonic-pdflib's People

Contributors

alexhayes avatar

Stargazers

 avatar Leora Bailey avatar

Watchers

 avatar James Cloos avatar Elias Zolotas avatar

Forkers

roi-com-au

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.