forked from anvilco/python-anvil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_pdf.py
More file actions
74 lines (55 loc) · 1.54 KB
/
generate_pdf.py
File metadata and controls
74 lines (55 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Run this from the project root
#
# ANVIL_API_KEY=YOUR_KEY python examples/generate_pdf.py && open ./generated.pdf
import os
from python_anvil.api import Anvil
from python_anvil.api_resources.payload import GeneratePDFPayload
API_KEY = os.environ.get("ANVIL_API_KEY")
# or set your own key here
# API_KEY = 'my-api-key'
def main():
anvil = Anvil(api_key=API_KEY)
data = html_data()
# You can specify data in literal dict form
# data = html_data_literal()
# Or you can generate from markdown
# data = markdown_data()
response = anvil.generate_pdf(data)
# Write the bytes to disk
with open('./generated.pdf', 'wb') as f:
f.write(response)
def html_data():
return GeneratePDFPayload(
type="html",
title="Some Title",
data=dict(
html="<h2>HTML Heading</h2>",
css="h2 { color: red }",
),
# Optional page configuration
# page=dict(
# width="8.5in",
# height="11in",
# ),
)
def html_data_literal():
return {
"type": "html",
"title": "Some Title",
"data": {
"html": "<h2>HTML Heading</h2>",
"css": "h2 { color: blue }",
},
}
def markdown_data():
return GeneratePDFPayload(
type="markdown",
title="Some Title",
data=[dict(label="Test", content="Lorem __Ipsum__")],
# Optional args
# font_size=10,
# font_family="Lobster",
# text_color="#cc0000",
)
if __name__ == '__main__':
main()