Skip to content

Commit 089d4e5

Browse files
Merge pull request #146 from chinapandaman/PPF-145
PPF-145: update docs with new font related features
2 parents 604112d + 10e33ba commit 089d4e5

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

docs/v2/api_reference.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ of PyPDFForm.
55

66
## PyPDFForm Object
77

8-
### *class* PyPDFForm.**PyPDFForm**(*template=b"", simple_mode=True, global_font_size=12, global_font_color=(0, 0, 0), global_text_x_offset=0, global_text_y_offset=0, global_text_wrap_length=100*)
8+
### *class* PyPDFForm.**PyPDFForm**(*template=b"", simple_mode=True, global_font="Helvetica", global_font_size=12, global_font_color=(0, 0, 0), global_text_x_offset=0, global_text_y_offset=0, global_text_wrap_length=100*)
99

1010
The PyPDFForm object implements a PDF form and acts as
1111
the central object. It can be constructed with or without a
@@ -22,6 +22,9 @@ by Python IO's `.read()` method.
2222
details like font size. Turning simple mode on also allows leaving PDF editable
2323
after filling.
2424

25+
* **global_font** - a string which sets the global font for text filled on the PDF form. The
26+
font set by this parameter has to be registered first. This will only take effect if `simple_mode` is `False`.
27+
2528
* **global_font_size** - an integer/float value which sets the global font size for texts
2629
filled on the PDF form. This will only take effect if `simple_mode` is `False`.
2730

@@ -65,7 +68,7 @@ on the specified page, coordinates with specified resolutions and rotation angle
6568

6669
* **rotation** - integer/float, degrees the image will be rotated after drawn.
6770

68-
### **draw_text**(*text, page_number, x, y, font_size=12, font_color=(0, 0, 0), text_x_offset=0, text_y_offset=0, text_wrap_length=100*)
71+
### **draw_text**(*text, page_number, x, y, font="Helvetica", font_size=12, font_color=(0, 0, 0), text_x_offset=0, text_y_offset=0, text_wrap_length=100*)
6972

7073
The draw text method takes a text string and draws it on the specified page
7174
at the specified coordinates.
@@ -80,6 +83,9 @@ at the specified coordinates.
8083

8184
* **y** - integer/float, vertical coordinate of which the text will be drawn at.
8285

86+
* **font** - string, sets the font for text drawn. Font set by this parameter has to be
87+
registered first.
88+
8389
* **font_size** - integer/float, font size of the text drawn.
8490

8591
* **font_color** - RGB integer/float tuple, font color of the text drawn.
@@ -112,6 +118,18 @@ be printed and a `boolean` will check the corresponding checkboxes.
112118

113119
* **editable** - only available if `simple_mode` is `True`, enabling this will allow the filled PDF to be still
114120
editable.
121+
122+
### **register_font**(*font_name, ttf_stream*)
123+
124+
This class method takes a TTF font file stream and register it with the `font_name` specified.
125+
Registered fonts can then be used by any instance of object.
126+
127+
#### Parameters:
128+
129+
* **font_name** - a string of which the font will be registered as. Registered fonts can be referenced and
130+
used via this name.
131+
132+
* **ttf_stream** - a byte stream of the ttf font file.
115133

116134
### **simple_mode** = *True*
117135

docs/v2/examples.md

+53
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,51 @@ with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
8686

8787
Link to this example: https://github.com/chinapandaman/PyPDFForm/blob/master/examples/simple_fill_editable.py
8888

89+
## Register font and set registered global font on filled text
90+
91+
This example registers a [LiberationSerif-Regular](https://github.com/chinapandaman/PyPDFForm/blob/master/font_samples/LiberationSerif-Regular.ttf)
92+
font and sets it as the global font on the filled PDF form.
93+
94+
```python
95+
import os
96+
97+
from PyPDFForm import PyPDFForm
98+
99+
PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM = os.path.join(
100+
os.path.expanduser("~/Downloads"), "sample_template.pdf"
101+
) # Change this to where you downloaded the sample PDF form
102+
103+
PATH_TO_SAMPLE_TTF_FONT_FILE = os.path.join(
104+
os.path.expanduser("~/Downloads"), "LiberationSerif-Regular.ttf"
105+
) # Change this to where you downloaded the sample font file
106+
107+
PATH_TO_FILLED_PDF_FORM = os.path.join(
108+
os.path.expanduser("~"), "output.pdf"
109+
) # Change this to where you wish to put your filled PDF form
110+
111+
with open(PATH_TO_SAMPLE_TTF_FONT_FILE, "rb+") as font:
112+
PyPDFForm.register_font("LiberationSerif-Regular", font.read())
113+
114+
with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
115+
filled_pdf = PyPDFForm(
116+
template.read(), simple_mode=False, global_font="LiberationSerif-Regular",
117+
).fill(
118+
{
119+
"test": "test_1",
120+
"check": True,
121+
"test_2": "test_2",
122+
"check_2": False,
123+
"test_3": "test_3",
124+
"check_3": True,
125+
},
126+
)
127+
128+
with open(PATH_TO_FILLED_PDF_FORM, "wb+") as output:
129+
output.write(filled_pdf.stream)
130+
```
131+
132+
Link to this example: https://github.com/chinapandaman/PyPDFForm/blob/master/examples/fill_font.py
133+
89134
## Set global font size and font color on filled text
90135

91136
This example sets a global font size of 20, and a global font color of red
@@ -354,10 +399,17 @@ PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM = os.path.join(
354399
os.path.expanduser("~/Downloads"), "sample_template.pdf"
355400
) # Change this to where you downloaded the sample PDF form
356401

402+
PATH_TO_SAMPLE_TTF_FONT_FILE = os.path.join(
403+
os.path.expanduser("~/Downloads"), "LiberationSerif-Italic.ttf"
404+
) # Change this to where you downloaded the sample font file
405+
357406
PATH_TO_FILLED_PDF_FORM = os.path.join(
358407
os.path.expanduser("~"), "output.pdf"
359408
) # Change this to where you wish to put your filled PDF form
360409

410+
with open(PATH_TO_SAMPLE_TTF_FONT_FILE, "rb+") as font:
411+
PyPDFForm.register_font("LiberationSerif-Italic", font.read())
412+
361413
with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
362414
pdf_form = PyPDFForm(template.read(), simple_mode=False)
363415

@@ -367,6 +419,7 @@ with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
367419
pdf_form.elements["test_2"].text_y_offset = -50
368420
pdf_form.elements["test_2"].text_wrap_length = 1
369421
pdf_form.elements["test_2"].font_color = (0, 1, 0)
422+
pdf_form.elements["test_3"].font = "LiberationSerif-Italic"
370423
pdf_form.elements["test_3"].text_wrap_length = 2
371424
pdf_form.elements["test_3"].font_color = (0, 0, 1)
372425

examples/fill_customized_elements.py

+8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
os.path.expanduser("~/Downloads"), "sample_template.pdf"
77
) # Change this to where you downloaded the sample PDF form
88

9+
PATH_TO_SAMPLE_TTF_FONT_FILE = os.path.join(
10+
os.path.expanduser("~/Downloads"), "LiberationSerif-Italic.ttf"
11+
) # Change this to where you downloaded the sample font file
12+
913
PATH_TO_FILLED_PDF_FORM = os.path.join(
1014
os.path.expanduser("~"), "output.pdf"
1115
) # Change this to where you wish to put your filled PDF form
1216

17+
with open(PATH_TO_SAMPLE_TTF_FONT_FILE, "rb+") as font:
18+
PyPDFForm.register_font("LiberationSerif-Italic", font.read())
19+
1320
with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
1421
pdf_form = PyPDFForm(template.read(), simple_mode=False)
1522

@@ -19,6 +26,7 @@
1926
pdf_form.elements["test_2"].text_y_offset = -50
2027
pdf_form.elements["test_2"].text_wrap_length = 1
2128
pdf_form.elements["test_2"].font_color = (0, 1, 0)
29+
pdf_form.elements["test_3"].font = "LiberationSerif-Italic"
2230
pdf_form.elements["test_3"].text_wrap_length = 2
2331
pdf_form.elements["test_3"].font_color = (0, 0, 1)
2432

examples/fill_font.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
3+
from PyPDFForm import PyPDFForm
4+
5+
PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM = os.path.join(
6+
os.path.expanduser("~/Downloads"), "sample_template.pdf"
7+
) # Change this to where you downloaded the sample PDF form
8+
9+
PATH_TO_SAMPLE_TTF_FONT_FILE = os.path.join(
10+
os.path.expanduser("~/Downloads"), "LiberationSerif-Regular.ttf"
11+
) # Change this to where you downloaded the sample font file
12+
13+
PATH_TO_FILLED_PDF_FORM = os.path.join(
14+
os.path.expanduser("~"), "output.pdf"
15+
) # Change this to where you wish to put your filled PDF form
16+
17+
with open(PATH_TO_SAMPLE_TTF_FONT_FILE, "rb+") as font:
18+
PyPDFForm.register_font("LiberationSerif-Regular", font.read())
19+
20+
with open(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM, "rb+") as template:
21+
filled_pdf = PyPDFForm(
22+
template.read(),
23+
simple_mode=False,
24+
global_font="LiberationSerif-Regular",
25+
).fill(
26+
{
27+
"test": "test_1",
28+
"check": True,
29+
"test_2": "test_2",
30+
"check_2": False,
31+
"test_3": "test_3",
32+
"check_3": True,
33+
},
34+
)
35+
36+
with open(PATH_TO_FILLED_PDF_FORM, "wb+") as output:
37+
output.write(filled_pdf.stream)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
setuptools.setup(
1818
name="PyPDFForm",
19-
version="0.2.0",
19+
version="0.2.1",
2020
description="python library for PDF forms",
2121
long_description=long_description,
2222
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)