summaryrefslogtreecommitdiffstats
path: root/docs/common_use_cases.rst
diff options
context:
space:
mode:
Diffstat (limited to 'docs/common_use_cases.rst')
-rw-r--r--docs/common_use_cases.rst89
1 files changed, 86 insertions, 3 deletions
diff --git a/docs/common_use_cases.rst b/docs/common_use_cases.rst
index 45e2d3d..d8343b3 100644
--- a/docs/common_use_cases.rst
+++ b/docs/common_use_cases.rst
@@ -97,7 +97,7 @@ Display image
document = pydyf.PDF()
- extra = Dictionary({
+ extra = pydyf.Dictionary({
'Type': '/XObject',
'Subtype': '/Image',
'Width': 197,
@@ -158,9 +158,9 @@ Display text
# And display it
text = pydyf.Stream()
text.begin_text()
- text.set_font_size('F1', 24)
+ text.set_font_size('F1', 20)
text.text_matrix(1, 0, 0, 1, 10, 90)
- text.show_text(pydyf.String('Hello World'))
+ text.show_text(pydyf.String('Bœuf grillé & café'.encode('macroman')))
text.end_text()
document.add_object(text)
@@ -180,3 +180,86 @@ Display text
with open('document.pdf', 'wb') as f:
document.write(f)
+
+Add metadata
+------------
+
+.. code-block:: python
+
+ import datetime
+
+ import pydyf
+
+ document = pydyf.PDF()
+ document.info['Author'] = pydyf.String('Jane Doe')
+ document.info['Creator'] = pydyf.String('pydyf')
+ document.info['Keywords'] = pydyf.String('some keywords')
+ document.info['Producer'] = pydyf.String('The producer')
+ document.info['Subject'] = pydyf.String('An example PDF')
+ document.info['Title'] = pydyf.String('A PDF containing metadata')
+ now = datetime.datetime.now()
+ document.info['CreationDate'] = pydyf.String(now.strftime('D:%Y%m%d%H%M%S'))
+
+ document.add_page(
+ pydyf.Dictionary(
+ {
+ 'Type': '/Page',
+ 'Parent': document.pages.reference,
+ 'MediaBox': pydyf.Array([0, 0, 200, 200]),
+ }
+ )
+ )
+
+ # 550 bytes PDF
+ with open('metadata.pdf', 'wb') as f:
+ document.write(f)
+
+
+Display inline QR-code image
+----------------------------
+
+.. code-block:: python
+
+ import pydyf
+ import qrcode
+
+ # Create a QR code image
+ image = qrcode.make('Some data here')
+ raw_data = image.tobytes()
+ width = image.size[0]
+ height = image.size[1]
+
+ document = pydyf.PDF()
+ stream = pydyf.Stream(compress=True)
+ stream.push_state()
+ x = 0
+ y = 0
+ stream.transform(width, 0, 0, height, x, y)
+ # Add the 1-bit grayscale image inline in the PDF
+ stream.inline_image(width, height, 'Gray', 1, raw_data)
+ stream.pop_state()
+ document.add_object(stream)
+
+ # Put the image in the resources of the PDF
+ document.add_page(
+ pydyf.Dictionary(
+ {
+ 'Type': '/Page',
+ 'Parent': document.pages.reference,
+ 'MediaBox': pydyf.Array([0, 0, 400, 400]),
+ 'Resources': pydyf.Dictionary(
+ {
+ 'ProcSet': pydyf.Array(
+ ['/PDF', '/ImageB', '/ImageC', '/ImageI']
+ ),
+ }
+ ),
+ 'Contents': stream.reference,
+ }
+ )
+ )
+
+ # 909 bytes PDF
+ with open('qrcode.pdf', 'wb') as f:
+ document.write(f, compress=True)
+