Creating PDF

PDF is certainly one of the most complicated formats out there. And certainly one of the formats most of the developers will be having trouble with, as there are many versions and dialects.

The goal of this library is to help you out with basic tasks. You can easily generate PDFs with 100 pages (paper size is A4), having a little text on each. Or you can generate PDFs with images. Currently, you can’t have both at the same time.

If you don’t like how PDF files are generated by this library, you can check the faker-file package, which can produce complex PDF documents.

Building PDF with text

If you need bytes

from fake import FAKER, TextPdfGenerator

pdf_bytes = FAKER.pdf(generator=TextPdfGenerator)

See the full example here

The generated PDF will consist of a single page with little text on it.

If you want to control number of pages created, you could:

  • Pass the list of texts to be used in texts argument.

  • Pass the number of pages to be created in nb_pages argument.


See the example below for texts tweak:

texts = FAKER.sentences()
pdf_bytes = FAKER.pdf(texts=texts, generator=TextPdfGenerator)

See the full example here


See the example below for nb_pages tweak:

pdf_bytes = FAKER.pdf(nb_pages=100, generator=TextPdfGenerator)

See the full example here

If you need files

pdf_file = FAKER.pdf_file(generator=TextPdfGenerator)

See the full example here


With texts tweak:

texts = FAKER.sentences()
pdf_file = FAKER.pdf_file(texts=texts, generator=TextPdfGenerator)

See the full example here


With nb_pages tweak:

pdf_file = FAKER.pdf_file(nb_pages=100, generator=TextPdfGenerator)

See the full example here

Building PDF with graphics

If you need bytes

from fake import FAKER, GraphicPdfGenerator

pdf_bytes = FAKER.pdf(generator=GraphicPdfGenerator)

See the full example here

The generated PDF will consist of a single page with a colored square on it.

If you want PDF with more pages, provide the nb_pages argument.


See the example below for nb_pages tweak:

pdf_bytes = FAKER.pdf(nb_pages=100, generator=GraphicPdfGenerator)

See the full example here

If you need files

pdf_file = FAKER.pdf_file(generator=GraphicPdfGenerator)

See the full example here


With nb_pages tweak:

pdf_file = FAKER.pdf_file(nb_pages=100, generator=GraphicPdfGenerator)

See the full example here