Skip to content

Commit

Permalink
Nicer whitespace in PDF objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Oct 5, 2012
1 parent 732f718 commit ba06e7e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions weasyprint/pdf.py
Expand Up @@ -200,10 +200,12 @@ def read_object(self, object_number):
assert int(line[:-7]) == object_number # len(b' 0 obj\n') == 7
object_lines = []
for line in fileobj:
object_lines.append(line)
if line == b'>>\n':
assert next(fileobj) == b'endobj\n'
# No newline, we’ll add it when writing.
object_lines.append(b'>>')
return b''.join(object_lines)
object_lines.append(line)

def overwrite_object(self, object_number, byte_string):
"""Write the new content for an existing object at the end of the file.
Expand All @@ -222,10 +224,10 @@ def extend_dict(self, dictionary, new_content):
the << >> delimiters.
"""
assert dictionary.byte_string.endswith(b'>>\n')
assert dictionary.byte_string.endswith(b'>>')
self.overwrite_object(
dictionary.object_number,
dictionary.byte_string[:-3] + new_content + b'\n>>\n')
dictionary.byte_string[:-2] + new_content + b'\n>>')

def next_object_number(self):
"""Return the object number that would be used by write_new_object().
Expand Down Expand Up @@ -384,7 +386,8 @@ def write_pdf_metadata(document, fileobj):
for bookmark in bookmarks:
content = [pdf_format('<< /Title {0!P}\n', bookmark['label'])]
content.append(pdf_format(
'/A << /Type /Action /S /GoTo /D [{0} /XYZ {1:f} {2:f} 0] >>',
'/A << /Type /Action /S /GoTo '
'/D [{0} /XYZ {1:f} {2:f} 0] >>\n',
*bookmark['target']))
if bookmark['Count']:
content.append(pdf_format('/Count {0}\n', bookmark['Count']))
Expand Down

0 comments on commit ba06e7e

Please sign in to comment.