forked from py-pdf/fpdf2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
110 additions
and
147 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,11 @@ | ||
# Combine with PyPDF2 | ||
|
||
`fpdf2` cannot **parse** existing PDF files. | ||
|
||
However, other Python libraries can be combined with `fpdf2` | ||
in order to add new content to existing PDF files. | ||
|
||
This page provides several examples of doing so using [`PyPDF2`](https://github.com/py-pdf/PyPDF2). | ||
|
||
## Adding content onto an existing PDF page | ||
In this code snippet, new content will be added on top of existing content: | ||
```python | ||
import io, sys | ||
|
||
from fpdf import FPDF | ||
from PyPDF2 import PdfReader, PdfWriter | ||
|
||
IN_FILEPATH = sys.argv[1] | ||
OUT_FILEPATH = sys.argv[2] | ||
ON_PAGE_INDEX = 0 # Index of the target page (starts at zero) | ||
|
||
def new_content(): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font('times', 'B', 30) | ||
pdf.text(50, 150, 'Hello World!') | ||
return pdf.output() | ||
|
||
reader = PdfReader(IN_FILEPATH) | ||
page_overlay = PdfReader(io.BytesIO(new_content())).getPage(0) | ||
reader.getPage(ON_PAGE_INDEX).merge_page(page2=page_overlay) | ||
|
||
writer = PdfWriter() | ||
writer.append_pages_from_reader(reader) | ||
writer.write(OUT_FILEPATH) | ||
``` | ||
|
||
## Adding a page to an existing PDF | ||
|
||
```python | ||
import io, sys | ||
|
||
from fpdf import FPDF | ||
from PyPDF2 import PdfMerger | ||
|
||
IN_FILEPATH = sys.argv[1] | ||
OUT_FILEPATH = sys.argv[2] | ||
ON_PAGE_INDEX = 2 # Index at which the page will be inserted (starts at zero) | ||
|
||
def new_page(): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font('times', 'B', 19) | ||
pdf.text(50, 10, 'Hello World!') | ||
return io.BytesIO(pdf.output()) | ||
|
||
merger = PdfMerger() | ||
merger.merge(position=0, fileobj=IN_FILEPATH) | ||
merger.merge(position=ON_PAGE_INDEX, fileobj=new_page()) | ||
merger.write(OUT_FILEPATH) | ||
``` | ||
|
||
## Altering with PyPDF2 a document generated with fpdf2 | ||
A document created with `fpdf2` can the be edited with `PyPDF2` | ||
by passing its `.output()` to a `PyPDF2.PdfReader`: | ||
```python | ||
import io | ||
from fpdf import FPDF | ||
from PyPDF2 import PdfReader | ||
|
||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font('times', 'B', 19) | ||
pdf.text(50, 10, 'Hello World!') | ||
|
||
reader = PdfReader(io.BytesIO(pdf.output())) | ||
``` | ||
<!DOCTYPE html> | ||
<html lang="en" > | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Redirecting to https://py-pdf.github.io/fpdf2/CombineWithPypdf.html</title> | ||
<meta http-equiv="refresh" content="0; URL=https://py-pdf.github.io/fpdf2/CombineWithPypdf.html"> | ||
<link rel="canonical" href="https://py-pdf.github.io/fpdf2/CombineWithPypdf.html"> | ||
</head> | ||
<body> | ||
This page moved to https://py-pdf.github.io/fpdf2/CombineWithPypdf.html | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Combine with pypdf | ||
|
||
`fpdf2` cannot **parse** existing PDF files. | ||
|
||
However, other Python libraries can be combined with `fpdf2` | ||
in order to add new content to existing PDF files. | ||
|
||
This page provides several examples of doing so using [`pypdf`](https://github.com/py-pdf/pypdf), an actively-maintained library formerly known as `PyPDF2`. | ||
|
||
## Adding content onto an existing PDF page | ||
In this code snippet, new content will be added on top of existing content: | ||
```python | ||
{% include "../tutorial/add_on_page_with_pypdf.py" %} | ||
``` | ||
|
||
## Adding a page to an existing PDF | ||
|
||
```python | ||
{% include "../tutorial/add_new_page_with_pypdf.py" %} | ||
``` | ||
|
||
## Altering with pypdf a document generated with fpdf2 | ||
A document created with `fpdf2` can the be edited with `pypdf` | ||
by passing its `.output()` to a `pypdf.PdfReader`: | ||
```python | ||
import io | ||
from fpdf import FPDF | ||
from pypdf import PdfReader | ||
|
||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font('times', 'B', 19) | ||
pdf.text(50, 10, 'Hello World!') | ||
|
||
reader = PdfReader(io.BytesIO(pdf.output())) | ||
``` |
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
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
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
3 changes: 0 additions & 3 deletions
3
tutorial/add_new_page.py → tutorial/add_new_page_with_pdfrw.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# USAGE: ./add_new_page.py $in_filepath $out_filepath | ||
|
||
import sys | ||
|
||
from fpdf import FPDF | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env python3 | ||
import io, sys | ||
|
||
from fpdf import FPDF | ||
from PyPDF2 import PdfMerger | ||
|
||
IN_FILEPATH = sys.argv[1] | ||
OUT_FILEPATH = sys.argv[2] | ||
ON_PAGE_INDEX = 2 # Index at which the page will be inserted (starts at zero) | ||
|
||
|
||
def new_page(): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("times", "B", 19) | ||
pdf.text(50, 10, "Hello World!") | ||
return io.BytesIO(pdf.output()) | ||
|
||
|
||
merger = PdfMerger() | ||
merger.merge(position=0, fileobj=IN_FILEPATH) | ||
merger.merge(position=ON_PAGE_INDEX, fileobj=new_page()) | ||
merger.write(OUT_FILEPATH) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env python3 | ||
import io, sys | ||
|
||
from fpdf import FPDF | ||
from pypdf import PdfReader, PdfWriter | ||
|
||
IN_FILEPATH = sys.argv[1] | ||
OUT_FILEPATH = sys.argv[2] | ||
ON_PAGE_INDEX = 0 # Index of the target page (starts at zero) | ||
|
||
|
||
def new_content(): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("times", "B", 30) | ||
pdf.text(50, 150, "Hello World!") | ||
return pdf.output() | ||
|
||
|
||
reader = PdfReader(IN_FILEPATH) | ||
page_overlay = PdfReader(io.BytesIO(new_content())).pages[0] | ||
reader.pages[ON_PAGE_INDEX].merge_page(page2=page_overlay) | ||
|
||
writer = PdfWriter() | ||
writer.append_pages_from_reader(reader) | ||
writer.write(OUT_FILEPATH) |