Last active
June 22, 2023 07:47
-
-
Save barseghyanartur/a5f3b205f3ae85983e8bc1fe47fe81d6 to your computer and use it in GitHub Desktop.
Create a PDF with 500 pages using `faker-file`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from faker import Faker | |
from faker_file.base import DynamicTemplate | |
from faker_file.providers.pdf_file import PdfFileProvider | |
from faker_file.providers.pdf_file.generators import reportlab_generator | |
from reportlab.lib.styles import getSampleStyleSheet | |
from reportlab.pdfbase import pdfmetrics | |
from reportlab.pdfbase.ttfonts import TTFont | |
from reportlab.platypus import PageBreak, Paragraph | |
FAKER = Faker() | |
FAKER.add_provider(PdfFileProvider) | |
# Add page break function | |
def pdf_add_page_break(provider, generator, story, data, counter, **kwargs): | |
# Insert a page break | |
story.append(PageBreak()) | |
# Add paragraph function | |
def pdf_add_paragraph(provider, generator, story, data, counter, **kwargs): | |
# Insert a paragraph | |
styles = getSampleStyleSheet() | |
style_paragraph = styles["Normal"] | |
style_paragraph.fontName = generator.font_name | |
pdfmetrics.registerFont(TTFont(generator.font_name, generator.font_path)) | |
content = provider.generator.text(max_nb_chars=5_000) | |
paragraph = Paragraph(content, style_paragraph) | |
story.append(paragraph) | |
# Create a file with text of 1_000 pages | |
pdf_file = FAKER.pdf_file( | |
pdf_generator_cls=reportlab_generator.ReportlabPdfGenerator, | |
content=DynamicTemplate( | |
[ | |
(pdf_add_paragraph, {}), | |
(pdf_add_page_break, {}), | |
] * 500 # Number of pages specified here | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
faker-file | |
reportlab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment