pythonlatexpylatex

How do I add a page background image in pylatex?


I have written the following code, and have given the latex commands for drawing background, by using the NoEscape module.

I have an image reportbg.png in the same directory as the program. Now, I want this image to appear as a background in all pages of the report.

types = ('../Faults/*.png', '../Faults/*.jpg')
imgnames = []
for files in types:
    imgnames.extend(natsort.natsorted(glob.glob(files)))

geometry_options = { "head": "30pt",
                 "margin": "0.3in",
                 "top": "0.2in",
                 "bottom": "0.4in",
                 "includeheadfoot": True}

doc = Document(geometry_options=geometry_options)
first_page = PageStyle("firstpage")
doc.preamble.append(first_page)
doc.change_document_style("firstpage")

new_comm1 = NoEscape(r'\usepackage{wallpaper}')
doc.append(new_comm1)
new_comm2 = NoEscape(r'\CenterWallPaper{reportbg.png}')
doc.append(new_comm2)

with doc.create(Section('Faults identified')):
    doc.append("Report")
    with doc.create(Subsection('Fault pictures')):
        for i,imgname in enumerate(imgnames): 
            with doc.create(Figure(position='h!')) as f_pic:
                f_pic.add_image(imgname, width='220px')
                f_pic.add_caption('Height: '+str(56)+', Angle: '+str(20))
                doc.append('Some regular text')

However, I got the following error:

! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...                                              

l.23 \usepackage
            {wallpaper}%
! Undefined control sequence.
l.24 \CenterWallPaper
                 {reportbg.png}%
<../Faults/1.jpg, id=1, 1927.2pt x 1084.05pt> <use ../Faults/1.jpg>
<../Faults/2.jpg, id=2, 1927.2pt x 1084.05pt> <use ../Faults/2.jpg>
<../Faults/3.jpg, id=3, 1927.2pt x 1084.05pt> <use ../Faults/3.jpg>
<../Faults/4.jpg, id=4, 1003.75pt x 1003.75pt> <use ../Faults/4.jpg>

LaTeX Warning: '!h' float specifier changed to '!ht'.

Solution

  • To implement a Background Image on all the pages of the document, you can generate first the PDF document in pylatex and then add the image as a watermark with PyPDF2. To do so, you need to have your 'reportbg.png' image into a pdf format (reportbg.pdf).

    Here's a modified example based on the pylatex documentation (https://jeltef.github.io/PyLaTeX/current/examples/basic.html):

    CODE

    from pylatex import Document, Section, Subsection, Command
    from pylatex.utils import italic, NoEscape
    import PyPDF2
    
    class Document_Watermark():
    
        def __init__(self, doc):
            self.doc = doc
            self.fill_document()
            self.create_document()
            self.Watermark()
    
        def fill_document(self):
            """Add a section, a subsection and some text to the document.
    
            :param doc: the document
            :type doc: :class:`pylatex.document.Document` instance
            """
            with self.doc.create(Section('A section')):
                self.doc.append('Some regular text and some ')
                self.doc.append(italic('italic text. '))
    
                with self.doc.create(Subsection('A subsection')):
                    self.doc.append('Also some crazy characters: $&#{}')
        def create_document(self):
            # Add stuff to the document
            with self.doc.create(Section('A second section')):
                self.doc.append('Some text.')
    
            self.doc.generate_pdf('basic_maketitle2', clean_tex=False, compiler='pdflatex')
            tex = self.doc.dumps()  # The document as string in LaTeX syntax
        def Watermark(self):
            Doc = open('basic_maketitle2.pdf', 'rb')
            pdfReader = PyPDF2.PdfFileReader(Doc)
            pdfWatermark = PyPDF2.PdfFileReader(open('watermark3.pdf', 'rb'))
            pdfWriter = PyPDF2.PdfFileWriter()
    
            for pageNum in range(0, pdfReader.numPages):
                pageObj = pdfReader.getPage(pageNum)
                pageObj.mergePage(pdfWatermark.getPage(0))
                pdfWriter.addPage(pageObj)
                resultPdfFile = open('PDF_Watermark.pdf', 'wb')
                pdfWriter.write(resultPdfFile)
            Doc.close()
            resultPdfFile.close()
    
    
    
    # Basic document
    doc = Document('basic')
    
    
    # Document with `\maketitle` command activated
    doc = Document()
    
    doc.preamble.append(Command('title', 'Awesome Title'))
    doc.preamble.append(Command('author', 'Anonymous author'))
    doc.preamble.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))
    
    Document_Watermark(doc)
    

    PS: The watermark, the initial generated pdf and the .py file must be at the same directory. I couldn't upload the PDF files, because this is my first answer post and I'm not really sure how could I, but I share some images. I hope it will be helpful. For more information, I suggest to read the following book: "Automate the boring stuff with Python", chapter 13, by Al Sweigart.