Skip to main content
Topic: Custom download script (watermark, DomPDF) (Read 1211 times) previous topic - next topic

Custom download script (watermark, DomPDF)

I'd like create a custom download PHP script:
When I download a PDF file from LIST page, my script (current location: helpers/download.php) take TEXT watermark to PDF with DomPDF.
Generating URLs: '<a href="' . SITE_ADDR . 'helpers/download.php?f=' . $data['file_url'] . '&w=' . $data['watermark'] . '">' . $data['watermark'] . '</a>'
Generated URLs: http://localhost/helpers/download.php?f=http://localhost/uploads/custom.pdf&w=watermark
How can I make this?
My script:
Code: [Select]
<?php
defined('ROOT') or exit('No direct script access allowed');
use Dompdf\Dompdf;
use Dompdf\Options;
use Dompdf\FontMetrics;

if (isset($_GET['f']) && isset($_GET['w'])) {
    $pdf = $_GET['f'];
    $wm = $_GET['w'];

    $options = new Options();
    $options->set('isPhpEnabled', 'true');
 
    $dompdf = new Dompdf($options);
    $dompdf->render();
    $canvas = $dompdf->getCanvas();
    $fontMetrics = new FontMetrics($canvas, $options);
 
    $w = $canvas->get_width();
    $h = $canvas->get_height();
 
    $font = $fontMetrics->getFont('times');
 
    $txtHeight = $fontMetrics->getFontHeight($font, 75);
    $textWidth = $fontMetrics->getTextWidth($wm, $font, 75);
 
    $canvas->set_opacity(.7);
 
    $x = 0.1*$w;
    $y = 0.9*$h;
 
    $canvas->text($x, $y, $wm, $font, 75);
    $dompdf->stream($pdf, array("Attachment" => 1));

} else {
    $pdf = NULL;
}
?>

 

Re: Custom download script (watermark, DomPDF)

Reply #1
The solution: Mpdf
Install Mpdf with Composer, and use this.