Skip to content

Commit 6a062e8

Browse files
committed
Fixed CreationDate of PDFs generated, that was broken - close #451
1 parent d2fa052 commit 6a062e8

File tree

299 files changed

+24
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

299 files changed

+24
-20
lines changed

CHANGELOG.md

+4-3

fpdf/fpdf.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from collections import OrderedDict, defaultdict
2727
from collections.abc import Sequence
2828
from contextlib import contextmanager
29-
from datetime import datetime
29+
from datetime import datetime, timezone
3030
from functools import wraps
3131
from math import isclose
3232
from os.path import splitext
@@ -434,15 +434,15 @@ def __init__(
434434
self.viewer_preferences = None
435435
self.compress = True # Enable compression by default
436436
self.pdf_version = "1.3" # Set default PDF version No.
437+
self.creation_date = True
437438

438439
self._current_draw_context = None
439440
self._drawing_graphics_state_registry = drawing.GraphicsStateDictRegistry()
440441
self._graphics_state_obj_refs = OrderedDict()
441442

442443
self.record_text_quad_points = False
443-
self.text_quad_points = defaultdict(
444-
list
445-
) # page number -> array of 8 × n numbers
444+
# page number -> array of 8 × n numbers:
445+
self.text_quad_points = defaultdict(list)
446446

447447
def _set_min_pdf_version(self, version):
448448
self.pdf_version = max(self.pdf_version, version)
@@ -681,7 +681,7 @@ def set_producer(self, producer):
681681

682682
def set_creation_date(self, date=None):
683683
"""Sets Creation of Date time, or current time if None given."""
684-
self.creation_date = datetime.now() if date is None else date
684+
self.creation_date = date
685685

686686
def set_xmp_metadata(self, xmp_metadata):
687687
if "<?xpacket" in xmp_metadata[:50]:
@@ -3219,7 +3219,7 @@ def image(
32193219
# disabling bandit rule as we just build a cache key, this is secure
32203220
name, img = hashlib.md5(bytes).hexdigest(), name # nosec B303 B324
32213221
elif isinstance(name, io.BytesIO):
3222-
bytes = name.getvalue()
3222+
bytes = name.getvalue().strip()
32233223
if _is_svg(bytes):
32243224
return self._vector_image(name, x, y, w, h, link, title, alt_text)
32253225
# disabling bandit rule as we just build a cache key, this is secure
@@ -4151,17 +4151,18 @@ def _putinfo(self):
41514151
"/Producer": enclose_in_parens(getattr(self, "producer", None)),
41524152
}
41534153

4154-
if hasattr(self, "creation_date"):
4154+
if self.creation_date is True:
4155+
# => no date has been specified, we use the current time by default:
4156+
self.creation_date = datetime.now(timezone.utc)
4157+
if self.creation_date:
41554158
try:
4156-
creation_date = self.creation_date
4157-
date_string = f"{creation_date:%Y%m%d%H%M%S}"
4159+
info_d["/CreationDate"] = enclose_in_parens(
4160+
f"D:{self.creation_date:%Y%m%d%H%M%SZ%H'%M'}"
4161+
)
41584162
except Exception as error:
41594163
raise FPDFException(
4160-
f"Could not format date: {creation_date}"
4164+
f"Could not format date: {self.creation_date}"
41614165
) from error
4162-
else:
4163-
date_string = f"{datetime.now():%Y%m%d%H%M%S}"
4164-
info_d["/CreationDate"] = enclose_in_parens(f"D:{date_string}")
41654166

41664167
self._out(pdf_dict(info_d, open_dict="", close_dict="", has_empty_fields=True))
41674168

test/add_page_duration.pdf

7 Bytes
Binary file not shown.

test/add_page_format.pdf

7 Bytes
Binary file not shown.

test/alias_nb_pages.pdf

7 Bytes
Binary file not shown.

test/barcodes/barcodes_code39.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/break_or_add_page.pdf

7 Bytes
Binary file not shown.

test/break_or_add_page_draw_fill.pdf

7 Bytes
Binary file not shown.

test/conftest.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ def assert_pdf_equal(actual, expected, tmp_path, generate=False):
6666
actual_pdf = actual.pdf
6767
else:
6868
actual_pdf = actual
69-
actual_pdf.set_creation_date(EPOCH)
69+
if (
70+
actual_pdf.creation_date is True
71+
): # default value, meaning we are not testing .creation_date behaviour:
72+
actual_pdf.set_creation_date(EPOCH)
7073
if generate:
7174
assert isinstance(expected, pathlib.Path), (
7275
"When passing `True` to `generate`"
7 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.

test/fonts/add_font_unicode.pdf

7 Bytes
Binary file not shown.

test/fonts/fonts_emoji_glyph.pdf

7 Bytes
Binary file not shown.

test/fonts/fonts_issue_66.pdf

7 Bytes
Binary file not shown.

test/fonts/fonts_remap_nb.pdf

7 Bytes
Binary file not shown.

test/fonts/fonts_set_builtin_font.pdf

7 Bytes
Binary file not shown.

test/fonts/fonts_two_mappings.pdf

7 Bytes
Binary file not shown.

test/fonts/render_en_dash.pdf

7 Bytes
Binary file not shown.

test/fonts/thai_text.pdf

7 Bytes
Binary file not shown.

test/goto_action.pdf

7 Bytes
Binary file not shown.

test/goto_next_page_chained.pdf

7 Bytes
Binary file not shown.

test/goto_remote_action.pdf

7 Bytes
Binary file not shown.

test/graphics_context.pdf

7 Bytes
Binary file not shown.

test/highlighted.pdf

7 Bytes
Binary file not shown.

test/highlighted_over_page_break.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/html/html_features.pdf

7 Bytes
Binary file not shown.

test/html/html_font_color_name.pdf

7 Bytes
Binary file not shown.

test/html/html_heading_hebrew.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/html/html_images.pdf

7 Bytes
Binary file not shown.

test/html/html_justify_paragraph.pdf

7 Bytes
Binary file not shown.

test/html/html_simple_table.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
Binary file not shown.

test/html/html_table_with_border.pdf

7 Bytes
Binary file not shown.
Binary file not shown.

test/html/issue_156.pdf

7 Bytes
Binary file not shown.

test/html/test_customize_ul.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.
Binary file not shown.

test/image/elliptic_clip.pdf

7 Bytes
Binary file not shown.

test/image/full_height_image.pdf

7 Bytes
Binary file not shown.

test/image/full_width_image.pdf

7 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/image/load_base64_data.pdf

7 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/image/rect_clip.pdf

7 Bytes
Binary file not shown.

test/image/round_clip.pdf

7 Bytes
Binary file not shown.

test/image/svg_image.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/image/svg_image_from_bytesio.pdf

7 Bytes
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/ink_annotation.pdf

7 Bytes
Binary file not shown.

test/launch_action.pdf

7 Bytes
Binary file not shown.

test/layout/set_no_margin.pdf

7 Bytes
Binary file not shown.

test/layout/unit_cm.pdf

7 Bytes
Binary file not shown.

test/layout/unit_default.pdf

7 Bytes
Binary file not shown.

test/layout/unit_float.pdf

7 Bytes
Binary file not shown.

test/layout/unit_in.pdf

7 Bytes
Binary file not shown.

test/layout/unit_int.pdf

7 Bytes
Binary file not shown.

test/layout/unit_mm.pdf

7 Bytes
Binary file not shown.

test/layout/unit_pt.pdf

7 Bytes
Binary file not shown.

test/link_alt_text.pdf

7 Bytes
Binary file not shown.

test/link_border.pdf

7 Bytes
Binary file not shown.

test/link_with_zoom_and_shift.pdf

7 Bytes
Binary file not shown.

test/links.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/local_context_init.pdf

7 Bytes
Binary file not shown.

test/local_context_shared_props.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/metadata/layout-alias-single.pdf

7 Bytes
Binary file not shown.

test/metadata/layout-alias-two.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/metadata/page-mode-USE_NONE.pdf

7 Bytes
Binary file not shown.

test/metadata/page-mode-USE_OC.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/metadata/put_info_all.pdf

7 Bytes
Binary file not shown.

test/metadata/put_info_some.pdf

7 Bytes
Binary file not shown.

test/metadata/setting_old_date.pdf

7 Bytes
Binary file not shown.

test/metadata/xmp_metadata.pdf

7 Bytes
Binary file not shown.

test/metadata/zoom-default.pdf

7 Bytes
Binary file not shown.

test/metadata/zoom-fullpage.pdf

7 Bytes
Binary file not shown.

test/metadata/zoom-fullwidth.pdf

7 Bytes
Binary file not shown.

test/metadata/zoom-real.pdf

7 Bytes
Binary file not shown.

test/named_actions.pdf

7 Bytes
Binary file not shown.

test/outline/2_pages_outline.pdf

7 Bytes
Binary file not shown.

test/outline/custom_HTML2FPDF.pdf

7 Bytes
Binary file not shown.

test/outline/html_toc.pdf

7 Bytes
Binary file not shown.

test/outline/html_toc_2_pages.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/outline/russian_heading.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/outline/simple_outline.pdf

-6.34 KB
Binary file not shown.

test/rotation.pdf

7 Bytes
Binary file not shown.

test/shapes/class_arc_clockwise.pdf

8 Bytes
Binary file not shown.

test/shapes/class_arc_draw_color.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/shapes/class_arc_fill_color.pdf

7 Bytes
Binary file not shown.

test/shapes/class_arc_inclination.pdf

7 Bytes
Binary file not shown.

test/shapes/class_arc_line_width.pdf

7 Bytes
Binary file not shown.

test/shapes/class_arc_not_circle.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/shapes/class_arc_style.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/shapes/class_circle_style.pdf

7 Bytes
Binary file not shown.

test/shapes/class_dash.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/shapes/class_ellipse_style.pdf

7 Bytes
Binary file not shown.

test/shapes/class_line.pdf

7 Bytes
Binary file not shown.

test/shapes/class_rect_draw_color.pdf

7 Bytes
Binary file not shown.

test/shapes/class_rect_fill_color.pdf

7 Bytes
Binary file not shown.

test/shapes/class_rect_line_width.pdf

7 Bytes
Binary file not shown.

test/shapes/class_rect_not_square.pdf

7 Bytes
Binary file not shown.

test/shapes/class_rect_style.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/shapes/class_solid_arc_style.pdf

7 Bytes
Binary file not shown.

test/shapes/dash_pattern.pdf

7 Bytes
Binary file not shown.

test/shapes/filled_polygon.pdf

7 Bytes
Binary file not shown.

test/shapes/regular_polygon.pdf

7 Bytes
Binary file not shown.

test/shapes/regular_star.pdf

7 Bytes
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.

test/svg/generated_pdf/SVG_logo.pdf

7 Bytes
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.

test/svg/generated_pdf/arcs01.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/arcs02.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/circle01.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/cubic01.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/cubic02.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/ellipse01.pdf

8 Bytes
Binary file not shown.

test/svg/generated_pdf/issue_358.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/issue_358b.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/line01.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/polygon01.pdf

8 Bytes
Binary file not shown.

test/svg/generated_pdf/polyline01.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/quad01.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/rect01.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/rect02.pdf

7 Bytes
Binary file not shown.

test/svg/generated_pdf/search.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/svg/generated_pdf/triangle01.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/svg/generated_pdf/viewbox.pdf

7 Bytes
Binary file not shown.

test/svg/svg_sources/use-xlink-href.svg

+2-3
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/template/flextemplate_offset.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/template/template_code39.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/template/template_justify.pdf

7 Bytes
Binary file not shown.

test/template/template_multipage.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/template/template_qrcode.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/template/template_textstyles.pdf

7 Bytes
Binary file not shown.

test/text/cell_centering.pdf

7 Bytes
Binary file not shown.

test/text/cell_ln_newpos.pdf

7 Bytes
Binary file not shown.

test/text/cell_markdown.pdf

7 Bytes
Binary file not shown.

test/text/cell_markdown_bleeding.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/text/cell_newpos.pdf

7 Bytes
Binary file not shown.

test/text/cell_table_unbreakable.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

test/text/cell_without_w_nor_h.pdf

7 Bytes
Binary file not shown.

test/text/clip_text_modes.pdf

7 Bytes
Binary file not shown.

test/text/ln_0.pdf

7 Bytes
Binary file not shown.

test/text/ln_1.pdf

7 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.

test/text/multi_cell_font_leakage.pdf

7 Bytes
Binary file not shown.

test/text/multi_cell_j_paragraphs.pdf

7 Bytes
Binary file not shown.

test/text/multi_cell_ln_1.pdf

7 Bytes
Binary file not shown.

test/text/multi_cell_ln_3.pdf

7 Bytes
Binary file not shown.

test/text/multi_cell_ln_3_table.pdf

7 Bytes
Binary file not shown.

test/text/multi_cell_ln_newpos.pdf

7 Bytes
Binary file not shown.

test/text/multi_cell_markdown.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.

test/text/multi_cell_newpos.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
Binary file not shown.
7 Bytes
Binary file not shown.

test/text/render_styled_newpos.pdf

7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/text/text_modes.pdf

7 Bytes
Binary file not shown.

test/text/write_page_break.pdf

7 Bytes
Binary file not shown.

test/text/write_soft_hyphen.pdf

7 Bytes
Binary file not shown.

test/text_annotation.pdf

7 Bytes
Binary file not shown.

test/transitions.pdf

7 Bytes
Binary file not shown.

test/transparency.pdf

7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)