Skip to content

Commit

Permalink
First example coded
Browse files Browse the repository at this point in the history
  • Loading branch information
sl2c committed Aug 3, 2023
1 parent cb35249 commit b0bee9b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions examples/example-pdfstream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3

from pdfrw import PdfReader, PdfWriter, PdfArray
from pdfrwx.pdffilter import PdfFilter
from pdfrwx.pdfstreamparser import PdfStream

toArray = lambda obj: obj if isinstance(obj,PdfArray) \
else PdfArray([obj]) if obj != None else PdfArray()

pdfIn = PdfReader('example.pdf')
pdfOut = PdfWriter('example-out.pdf')
for page in pdfIn.pages:
contentsArray = toArray(page.Contents)
for contents in contentsArray:
stream = PdfFilter.uncompress(contents).stream
treeIn = PdfStream.stream_to_tree(stream)
treeOut = []
for leaf in treeIn:
cmd, args = leaf[:2]
if cmd != 'BT': treeOut.append(leaf)
contents.stream = PdfStream.tree_to_stream(treeOut)
contents.Filter = None
pdfOut.addPage(page)
pdfOut.write()
Binary file added examples/example.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion pdffont.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def __init__(self, fontDict:PdfDict, glyphMap:PdfFontGlyphMap = PdfFontGlyphMap(

def install(self, pdfPage, fontName, overwrite=False):
'''
Adds self.fontdict to the pdfPage.Resources.Font dictionary under the name fontName.
Adds self.font to the pdfPage.Resources.Font dictionary under the name fontName.
The font can then be referred to using Tf operators in the pdfPage.stream
'''
if pdfPage.Resources == None: pdfPage.Resources = PdfDict()
Expand Down
2 changes: 1 addition & 1 deletion pdfimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def encode(image:Image, quality = 'keep'):
filter = 'DCTDecode'
else:
pass
# msg('JPEG re-compressions skiped; ' + print_size_change(size, size_compressed))
# msg('JPEG re-compressions skipped; ' + print_size_change(size, size_compressed))

# PDF expects inverted CMYK JPEGs
if image.mode == 'CMYK' and filter == 'DCTDecode':
Expand Down

0 comments on commit b0bee9b

Please sign in to comment.