The PdfFileWriter Class
- class PyPDF2.pdf.PdfFileWriter[source]
Bases:
object
This class supports writing PDF files out, given pages produced by another class (typically
PdfFileReader
).- addAttachment(fname, fdata)[source]
Embed a file inside the PDF.
- Parameters
fname (str) – The filename to display.
fdata (str) – The data in the file.
Reference: https://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf Section 7.11.3
- addBlankPage(width=None, height=None)[source]
Append a blank page to this PDF file and returns it. If no page size is specified, use the size of the last page.
- Parameters
width (float) – The width of the new page expressed in default user space units.
height (float) – The height of the new page expressed in default user space units.
- Returns
the newly appended page
- Return type
- Raises
PageSizeNotDefinedError – if width and height are not defined and previous page does not exist.
- addBookmark(title, pagenum, parent=None, color=None, bold=False, italic=False, fit='/Fit', *args)[source]
Add a bookmark to this PDF file.
- Parameters
title (str) – Title to use for this bookmark.
pagenum (int) – Page number this bookmark will point to.
parent – A reference to a parent bookmark to create nested bookmarks.
color (tuple) – Color of the bookmark as a red, green, blue tuple from 0.0 to 1.0
bold (bool) – Bookmark is bold
italic (bool) – Bookmark is italic
fit (str) – The fit of the destination page. See
addLink()
for details.
- addJS(javascript)[source]
Add Javascript which will launch upon opening this PDF.
- Parameters
javascript (str) – Your Javascript.
>>> output.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});") # Example: This will launch the print window when the PDF is opened.
- addLink(pagenum, pagedest, rect, border=None, fit='/Fit', *args)[source]
Add an internal link from a rectangular area to the specified page.
- Parameters
pagenum (int) – index of the page on which to place the link.
pagedest (int) – index of the page to which the link should go.
rect –
RectangleObject
or array of four integers specifying the clickable rectangular area[xLL, yLL, xUR, yUR]
, or string in the form"[ xLL yLL xUR yUR ]"
.border – if provided, an array describing border-drawing properties. See the PDF spec for details. No border will be drawn if this argument is omitted.
fit (str) – Page fit or ‘zoom’ option (see below). Additional arguments may need to be supplied. Passing
None
will be read as a null value for that coordinate.
/Fit
No additional arguments
/XYZ
[left] [top] [zoomFactor]
/FitH
[top]
/FitV
[left]
/FitR
[left] [bottom] [right] [top]
/FitB
No additional arguments
/FitBH
[top]
/FitBV
[left]
- addMetadata(infos)[source]
Add custom metadata to the output.
- Parameters
infos (dict) – a Python dictionary where each key is a field and each value is your new metadata.
- addPage(page)[source]
Add a page to this PDF file. The page is usually acquired from a
PdfFileReader
instance.- Parameters
page (PageObject) – The page to add to the document. Should be an instance of
PageObject
- addURI(pagenum, uri, rect, border=None)[source]
Add an URI from a rectangular area to the specified page. This uses the basic structure of AddLink
- Parameters
pagenum (int) – index of the page on which to place the URI action.
uri (int) – string – uri of resource to link to.
rect –
RectangleObject
or array of four integers specifying the clickable rectangular area[xLL, yLL, xUR, yUR]
, or string in the form"[ xLL yLL xUR yUR ]"
.border – if provided, an array describing border-drawing properties. See the PDF spec for details. No border will be drawn if this argument is omitted.
REMOVED FIT/ZOOM ARG -John Mulligan
- appendPagesFromReader(reader, after_page_append=None)[source]
Copy pages from reader to writer. Includes an optional callback parameter which is invoked after pages are appended to the writer.
- Parameters
reader – a PdfFileReader object from which to copy page annotations to this writer object. The writer’s annots will then be updated
reference) (writer_pageref (PDF page) – Reference to the page appended to the writer.
- Callback after_page_append (function)
Callback function that is invoked after each page is appended to the writer. Callback signature:
- cloneDocumentFromReader(reader, after_page_append=None)[source]
Create a copy (clone) of a document from a PDF file reader
- Parameters
reader – PDF file reader instance from which the clone should be created.
- Callback after_page_append (function)
Callback function that is invoked after each page is appended to the writer. Signature includes a reference to the appended page (delegates to appendPagesFromReader). Callback signature:
- param writer_pageref (PDF page reference)
Reference to the page just appended to the document.
- cloneReaderDocumentRoot(reader)[source]
Copy the reader document root to the writer.
- Parameters
reader – PdfFileReader from the document root should be copied.
- Callback after_page_append
- encrypt(user_pwd, owner_pwd=None, use_128bit=True, permissions_flag=- 1)[source]
Encrypt this PDF file with the PDF Standard encryption handler.
- Parameters
user_pwd (str) – The “user password”, which allows for opening and reading the PDF file with the restrictions provided.
owner_pwd (str) – The “owner password”, which allows for opening the PDF files without any restrictions. By default, the owner password is the same as the user password.
use_128bit (bool) – flag as to whether to use 128bit encryption. When false, 40bit encryption will be used. By default, this flag is on.
permissions_flag (unsigned int) – permissions as described in TABLE 3.20 of the PDF 1.7 specification. A bit value of 1 means the permission is grantend. Hence an integer value of -1 will set all flags. Bit position 3 is for printing, 4 is for modifying content, 5 and 6 control annotations, 9 for form fields, 10 for extraction of text and graphics.
- getPage(pageNumber)[source]
Retrieve a page by number from this PDF file.
- Parameters
pageNumber (int) – The page number to retrieve (pages begin at zero)
- Returns
the page at the index given by pageNumber
- Return type
PageObject
- getPageLayout()[source]
Get the page layout.
See
setPageLayout()
for a description of valid layouts.- Returns
Page layout currently being used.
- Return type
str, None if not specified
- getPageMode()[source]
Get the page mode. See
setPageMode()
for a description of valid modes.- Returns
Page mode currently being used.
- Return type
str, None if not specified.
- insertBlankPage(width=None, height=None, index=0)[source]
Insert a blank page to this PDF file and returns it. If no page size is specified, use the size of the last page.
- Parameters
width (float) – The width of the new page expressed in default user space units.
height (float) – The height of the new page expressed in default user space units.
index (int) – Position to add the page.
- Returns
the newly appended page
- Return type
- Raises
PageSizeNotDefinedError – if width and height are not defined and previous page does not exist.
- insertPage(page, index=0)[source]
Insert a page in this PDF file. The page is usually acquired from a
PdfFileReader
instance.- Parameters
page (PageObject) – The page to add to the document. This argument should be an instance of
PageObject
.index (int) – Position at which the page will be inserted.
- property pageLayout
Read and write property accessing the
getPageLayout()
andsetPageLayout()
methods.
- property pageMode
Read and write property accessing the
getPageMode()
andsetPageMode()
methods.
- removeImages(ignoreByteStringObject=False)[source]
Remove images from this output.
- Parameters
ignoreByteStringObject (bool) – optional parameter to ignore ByteString Objects.
- removeText(ignoreByteStringObject=False)[source]
Remove text from this output.
- Parameters
ignoreByteStringObject (bool) – optional parameter to ignore ByteString Objects.
- setPageLayout(layout)[source]
Set the page layout.
- Parameters
layout (str) – The page layout to be used.
/NoLayout
Layout explicitly not specified
/SinglePage
Show one page at a time
/OneColumn
Show one column at a time
/TwoColumnLeft
Show pages in two columns, odd-numbered pages on the left
/TwoColumnRight
Show pages in two columns, odd-numbered pages on the right
/TwoPageLeft
Show two pages at a time, odd-numbered pages on the left
/TwoPageRight
Show two pages at a time, odd-numbered pages on the right
- setPageMode(mode)[source]
Set the page mode.
- Parameters
mode (str) – The page mode to use.
/UseNone
Do not show outlines or thumbnails panels
/UseOutlines
Show outlines (aka bookmarks) panel
/UseThumbs
Show page thumbnails panel
/FullScreen
Fullscreen view
/UseOC
Show Optional Content Group (OCG) panel
/UseAttachments
Show attachments panel
- updatePageFormFieldValues(page, fields, flags=0)[source]
Update the form field values for a given page from a fields dictionary. Copy field texts and values from fields to page. If the field links to a parent object, add the information to the parent.
- Parameters
page – Page reference from PDF writer where the annotations and field data will be updated.
fields – a Python dictionary of field names (/T) and text values (/V)
flags – An integer (0 to 7). The first bit sets ReadOnly, the second bit sets Required, the third bit sets NoExport. See PDF Reference Table 8.70 for details.