parent
fd0ed71136
commit
8b8478231e
@ -0,0 +1 @@ |
||||
include README.md |
@ -1,2 +1,11 @@ |
||||
# html_to_pdf |
||||
|
||||
### API |
||||
|
||||
```python |
||||
import html_to_pdf as h2p |
||||
|
||||
h2p.render_from_url("http://example.com", "out.pdf") |
||||
h2p.render_from_file("local_file.html", "out2.pdf") |
||||
h2p.render_from_string("<h1>Works</h1>", "out3.pdf") |
||||
``` |
||||
|
@ -0,0 +1 @@ |
||||
from .render import render_from_url, render_from_string, render_from_file |
@ -0,0 +1,41 @@ |
||||
import os |
||||
import asyncio |
||||
import tempfile |
||||
from pyppeteer import launch |
||||
|
||||
|
||||
def sync_wait(future): |
||||
loop = asyncio.get_event_loop() |
||||
return loop.run_until_complete(future) |
||||
|
||||
|
||||
def render_from_url(url, out=None): |
||||
if out is None: |
||||
out = "out.pdf" |
||||
out = os.path.join(os.getcwd(), out) |
||||
|
||||
browser = sync_wait(launch()) |
||||
page = sync_wait(browser.newPage()) |
||||
sync_wait(page.goto(url)) |
||||
sync_wait(page.pdf({ |
||||
"displayHeaderFooter":False, |
||||
"printBackground": True, |
||||
"path": out, |
||||
"preferCSSPageSize": True, |
||||
})) |
||||
sync_wait(browser.close()) |
||||
|
||||
|
||||
def render_from_file(s, out=None): |
||||
if s[0] != "/": |
||||
s = os.path.join(os.getcwd(), s) |
||||
f = 'file://' + s |
||||
render_from_url(f, out=out) |
||||
|
||||
|
||||
def render_from_string(s, out=None): |
||||
temp = tempfile.NamedTemporaryFile(suffix=".html", mode="w") |
||||
temp.write(s) |
||||
temp.flush() |
||||
render_from_file(temp.name, out=out) |
||||
temp.close() |
@ -0,0 +1,20 @@ |
||||
import setuptools |
||||
|
||||
with open("README.md", "r") as fh: |
||||
long_description = fh.read() |
||||
|
||||
setuptools.setup( |
||||
name="html_to_pdf", |
||||
version="0.2", |
||||
install_requires=[ |
||||
'pyppeteer' |
||||
], |
||||
author="Ugo Finnendahl", |
||||
author_email="ugo@finnendahl.de", |
||||
description="Convert html to pdf", |
||||
long_description=long_description, |
||||
long_description_content_type="text/markdown", |
||||
url="git.finnendahl.de", |
||||
packages=setuptools.find_packages(), |
||||
include_package_data=True |
||||
) |
Loading…
Reference in new issue