GithubHelp home page GithubHelp logo

Comments (5)

jbarlow83 avatar jbarlow83 commented on June 14, 2024

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.

DanielBalsam avatar DanielBalsam commented on June 14, 2024

Thanks for the response!

Same problem unfortunately. I am on the latest version.

from pikepdf.

jbarlow83 avatar jbarlow83 commented on June 14, 2024

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.

jbarlow83 avatar jbarlow83 commented on June 14, 2024

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.

jbarlow83 avatar jbarlow83 commented on June 14, 2024

Closing due to age

from pikepdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.