Comments (5)
What happens if you try...
if idx == 0:
watermark_file = Pdf.open('watermark.pdf')
watermark = watermark_file.pages[0]
watermark_cs = watermark.Contents.read_bytes()
for pdf_page in pdf.pages:
pdf_page.page_contents_add(contents=watermark_cs)
Also, is this the latest version?
from pikepdf.
Thanks for the response!
Same problem unfortunately. I am on the latest version.
from pikepdf.
The code sample is not executable - some missing variables and definitions. So I am to some extent guessing at the intent.
I'm not sure how it's able to execute, because my approximate to it immediately gave this error:
TypeError: page_contents_add(): incompatible function arguments. The following argument types are supported:
1. (self: pikepdf._qpdf.Object, contents: pikepdf._qpdf.Object, prepend: bool=False) -> None
It is no longer necessary to hold source files open. You can do it more like this:
import pikepdf
from pikepdf import Pdf
import io
wm = 'tests/resources/formxobject.pdf'
def test(input_files, output_stream):
merger = Pdf.new()
for idx, input_file in enumerate(input_files):
with Pdf.open(input_file) as pdf:
if idx == 0:
watermark_file = Pdf.open(wm)
watermark = watermark_file.pages[0]
for pdf_page in pdf.pages:
pdf_page.page_contents_add(contents=watermark.Contents)
merger.pages.extend(pdf.pages)
merger.save(output_stream)
return True
input_files = ['tests/resources/sandwich.pdf', 'tests/resources/graph.pdf']
with open('issue43.pdf', 'wb') as output_stream:
test(input_files, output_stream)
Another point to note is that unless the watermark is entirely vector art, it will not transfer when used that way. You have to transfer any resources referenced in the content stream as well, such as fonts or images, to the target page. The optimal way to do this is use QPDF's new feature to convert a page to a Form XObject (a "page in a box") but I haven't had a chance to integrate that API. Have at look weave.py in ocrmypdf for an example of watermarking pages with text.
from pikepdf.
Please check again with the latest version. It's also possible this was related to a GIL issue that was fixed in a dependency.
from pikepdf.
Closing due to age
from pikepdf.
Related Issues (20)
- 8.1.1: tests/test_image_access.py::test_image_palette2 - hypothesis.errors.Flaky HOT 1
- Bad link HOT 1
- Unable to install on alpine - dependency issue with qpdf HOT 7
- datetime.utcnow is deprecated in Python 3.12 HOT 1
- How to randomly shuffle the position of objects without affecting the display of content? HOT 1
- test_stack_depth fails on Python 3.12 HOT 3
- Docs are no longer in sdist
- Error: ‘writeQPDF’ is not a member of ‘QPDFJob’ HOT 1
- Calling check() after generate_appearance_streams()
- Add color output intent profile to PDF
- Add color output intent profile to PDF HOT 4
- Cannot save PDF opened from BytesIO on Windows
- PDF image extraction error: Not sure how to handle PDF image of this type HOT 3
- PDF.check() does not report warnings when qpdf --check does HOT 1
- UnsupportedImageTypeError('/CCITTFaxDecode with decode parameter /EndOfBlock not equal True') when converting PDF to image HOT 3
- Use the Job API's logging functions to redirect to logging rather than stdout/stderr
- Unable open RC4 encrypted pdf HOT 17
- Mypy implicit reexport HOT 2
- First tutorial example is unclear: new_pdf?
- The password param in method ‘open’ HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pikepdf.