Skip to content

Watermark don't delete background files in /tmp #826

@Joao-Moura

Description

@Joao-Moura

Describe the Bug

When using background-image in @page, after a long period of generating PDFs, the /tmp file starts to get increasingly larger, causing disk storage problems.

Minimal Example to Reproduce

import os
from xhtml2pdf import pisa

before_pdf = os.listdir('/tmp')

html_source = """
<html>
    <head>
        <style type="text/css">
            @page {
                size: a4 portrait;
		background-image: url('files/dummy_background.jpg');
		background-opacity: 0.1;
            }
        </style>
    </head>
    <body>
        <h1>Example</h1>
    </body>
</html>
"""

with open('test.pdf', 'wb') as f:
    pisa.CreatePDF(html_source, dest=f)

after_pdf = os.listdir('/tmp')
assert before_pdf == after_pdf

Expected Behavior

The assertion should not fail and the temporary files created in /tmp should be deleted.

Actual Behavior

The assertion fails and the /tmp folder (ls -la /tmp) will contain increasingly new temporary files (dummy_background.jpg).

Additional Information

[Edited] -> For the code on the master branch:
I believe that this problem is caused on process_doc function, as the returned NamedTemporaryFile from get_watermark -> generate_pdf_background -> pisaFileObject -> LocalTmpFile -> get_named_tmp_file is never .closed() (or finalized).

For the code on release v0.2.17:
I still believe the problem is related with generate_pdf_background generating NamedTemporaryFile that are not closed after use, but as I am exploring the code from this version to make a monkey patch, it seems the problem is in the beforeDrawPage function.

System Information

OS version: Debian 11-slim
Python version: 3.10
XHTML2PDF version: 0.2.17

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions