Skip to content

Temporary PDF files generated by getOutputFromHtml() are not removed #554

Description

@mkurniawandhariyadi

Hello KnpLabs, i jave some issue with detail

Environment

OS: Fedora Server 37
PHP: 8.4.x
Knp Snappy: v1.7.2
wkhtmltopdf: 0.12.6
Apache + PHP-FPM
CodeIgniter 4

Description

When using Pdf::getOutputFromHtml(), temporary PDF files remain in the temporary directory after the request completes.

Example:

$snappy = new Pdf('/usr/bin/wkhtmltopdf');

$output = $snappy->getOutputFromHtml(
    '<html><body>Hello World</body></html>'
);

echo strlen($output);

Expected behavior

Temporary files created by Snappy should be removed automatically after the PDF output has been returned.

Actual behavior

Files such as:

/tmp/knp_snappyxxxxxxxx.pdf

remain in the temporary directory and accumulate over time.

Investigation

Looking at AbstractGenerator.php getOutput() creates an output filename using:

$filename = $this->createTemporaryFile(
    null,
    $this->getDefaultExtension()
);

Inside createTemporaryFile():

if (null !== $content) {
    file_put_contents($filename, $content);
    $this->temporaryFiles[] = $filename;
}

Since the PDF output file is created with $content = null, it is not added to $this->temporaryFiles.

removeTemporaryFiles() only processes files stored in $this->temporaryFiles.

Could this explain why temporary PDF files remain after successful execution, or am I missing another cleanup mechanism?

Thanks

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions