Skip to content

Commit

Permalink
improve readme
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed May 23, 2017
1 parent 05dd613 commit 75515bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 43 deletions.
57 changes: 21 additions & 36 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PDF generation in python using
Wkhtmltopdf binaries are precompiled and included in the package making
pydf easier to use, in particular this means pydf works on heroku.

Currently using **wkhtmltopdf 0.12.4 (with patched qt)**.
Currently using **wkhtmltopdf 0.12.4 (with patched qt)**, requires **Python 3.6+**.

Install
-------
Expand All @@ -19,7 +19,7 @@ Install

pip install python-pdf

(pydf was taken, but I guess python-pdf is a clearer name anyway.)
For python 2 use ``pip install python-pdf==0.30.0``.

Basic Usage
-----------
Expand All @@ -28,11 +28,7 @@ Basic Usage
import pydf
pdf = pydf.generate_pdf('<h1>this is html</h1>')
with open('test_doc.pdf', 'w') as f:
f.write(pdf)
pdf = pydf.generate_pdf('www.google.com')
with open('google.pdf', 'w') as f:
with open('test_doc.pdf', 'wb') as f:
f.write(pdf)
Async Usage
Expand All @@ -44,38 +40,28 @@ at the same time. Thus the time taken to spin up processes doesn't slow you down

.. code:: python
async def go_async():
count = 20
apydf = AsyncPydf(max_processes=20)
async def gen(i_):
pdf = await apydf.generate_pdf(
html,
title='Benchmark',
author='Samuel Colvin',
subject='Mock Invoice',
page_size='A4',
zoom='1.25',
margin_left='8mm',
margin_right='8mm',
)
print(f'{i_:03}: {len(pdf)}')
file = OUT_DIR / f'output_{i_:03}.pdf'
file.write_bytes(pdf)
coros = []
for i in range(count):
coros.append(gen(i))
await asyncio.gather(*coros)
return count
loop = asyncio.get_event_loop()
loop.run_until_complete(go_async())
from pathlib import Path
from pydf import AsyncPydf
async def generate_async():
apydf = AsyncPydf()
async def gen(i):
pdf_content = await apydf.generate_pdf('<h1>this is html</h1>')
Path(f'output_{i:03}.pdf').write_bytes(pdf_content)
coros = [gen(i) for i in range(50)]
await asyncio.gather(*coros)
loop = asyncio.get_event_loop()
loop.run_until_complete(generate_async())
See `benchmarks/run.py <https://github.com/tutorcruncher/pydf/blob/master/benchmark/run.py>`__
for a full example.

Locally generating an entire invoice goes from 0.372s/pdf to 0.035s/pdf with the async model.

API
---

Expand All @@ -90,8 +76,7 @@ For details on extra arguments see the output of get\_help() and
get\_extended\_help()

All arguments whether specified or caught with extra\_kwargs are
converted to command line args with
``'--' + original_name.replace('_', '-')``.
converted to command line args with ``'--' + original_name.replace('_', '-')``.

Arguments which are True are passed with no value eg. just --quiet,
False and None arguments are missed, everything else is passed with
Expand Down
12 changes: 5 additions & 7 deletions benchmark/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def go_sync():
print(f'sync, time taken per pdf: {time_taken:0.3f}s')

async def go_async():
count = 20
apydf = AsyncPydf(max_processes=20)
apydf = AsyncPydf()

async def gen(i_):
pdf = await apydf.generate_pdf(
Expand All @@ -55,12 +54,11 @@ async def gen(i_):
margin_right='8mm',
)
print(f'{i_:03}: {len(pdf)}')
file = OUT_DIR / f'output_{i_:03}.pdf'
file.write_bytes(pdf)
f = OUT_DIR / f'output_{i_:03}.pdf'
f.write_bytes(pdf)

coros = []
for i in range(count):
coros.append(gen(i))
count = 20
coros = map(gen, range(count))
await asyncio.gather(*coros)
return count

Expand Down

0 comments on commit 75515bf

Please sign in to comment.