django-wkhtmltopdf icon indicating copy to clipboard operation
django-wkhtmltopdf copied to clipboard

How to print static image as watermark for every page in pdf document

Open eruiz67 opened this issue 2 years ago • 0 comments

I need to be able to use a static image as watermark when printing a pdf from a django template. How can I do it using django-wkhtml2pdf? I was trying using the following code, but the document has several pages and the background does not apply to every page individually. Instead it tries to apply the background to the whole document, and as a result the images takes a bigger size, covering all pages. How can I apply the image as a background to every page individually? Here is my code:

Defining a background using CSS

body { background-image: url("{{ watermark_logos }}"); background-size: cover; }

Generating a pdf with django-wkhtml2pdf. Note that the "cmd_options" represent the parameters for wkhtmltopdf
response = PDFTemplateResponse(request=request,
                                  template=template_to_use,
                                  filename="Ficha proyecto {}.pdf".format(project.get_numero_trabajo),
                                  context= data,
                                  show_content_in_browser=False,
                                  cmd_options={  
                                   'margin-top':0,
                                   'margin-bottom':0,
                                   'margin-left':0,
                                   'margin-right':0,
                                  "zoom":1,
                                  "viewport-size" :"1366x513",
                                  'javascript-delay':1000,
                                  'enable-local-file-access':True,
                                  'footer-center' :'[page]/[topage]',
                                  "no-stop-slow-scripts":True},
                                  )

Here is the watermark I want to use for every page

MARCA-DE-AGUA-SIN-LOGOS-02

eruiz67 avatar Feb 22 '23 23:02 eruiz67