DOMPDF 仅在最后一页显示图像水印

问题描述 投票:0回答:2

我尝试使用 DomPdf 显示图像水印,但它只出现在最后一页。我浏览了一些网站,但没有找到任何相关内容。

这是我的代码:

<?php   
    require_once 'dompdf/autoload.inc.php';

    //referenciar o DomPDF com namespace
    use Dompdf\Dompdf;
    //Referenciar o NameSpace Options
    use Dompdf\Options;


    //Definir Options para habilitar PHP incorporado.
    $options = new Options();
    $options->set('isPhpEnabled', 'true');

    //Criando a Instancia
    $dompdf = new Dompdf($options);

    // Carrega seu HTML
    $dompdf->loadhtml("<h3>https://www.TutorialsWebsite.com</h3><center><h1>Welcome to Tutorials Website</h1><div class="wpb_wrapper">
            <p><strong>Tutorialswebsite</strong> is a leading online education portal that helps technologies, software, business and creative skills readers to learn new skills at their own place from the comforts of their drawing rooms. Individual, corporate and academic members have access to learn anything on <a href="https://www.tutorialswebsite.com">tutorialswebsite.com</a> likes video tutorials, blogs content etc.</p>
<p>From 5 years, we worked our way to adding new fresh articles on topics ranging from programming languages that helps students, leaders, IT and design professionals, project managers or anyone who is working with software development, creatives and business skills.</p>
<h2 style="text-align: center;">Mission</h2>
<p>Our mission is to deliver easy and best online resources on programming and web development to encourage our readers acquire as many skills as they would like to. We offer the useful and best tutorials for web professionals-developers, programmers, freelancer free of cost. We don’t force our readers to sign up with us or submit their details.</p>
 
        </div><h2 style="font-size: 24px;text-align: center;font-family:Roboto;font-weight:700;font-style:normal" class="vc_custom_heading">About Author</h2>
    <p>
<strong><a href="https://www.pradeepmaurya.in/">Pradeep Maurya</a></strong> is the Professional Web Developer and Founder of &nbsp;<a href="https://www.tutorialswebsite.com">“Tutorialswebsite”</a>. He lives in Delhi and loves to be a self dependent person. As an author, he is trying his best to improve this platform day by day.
You can contact him <strong><a href="https://www.facebook.com/erpradeepmauryarkt" target="_blank" rel="noopener noreferrer">@facebook</a></strong><strong>Website:</strong> <a href="https://www.pradeepmaurya.in" target="_blank" rel="noopener noreferrer">https://www.pradeepmaurya.in</a></p></center>
'");

    
    $dompdf->setPaper('A4', 'portrait');
    
    //Renderizar o html
    $dompdf->render();  

    //Instanciar o Canvas
    $canvas = $dompdf->getCanvas();

    //Recuperar Altura e Largura da página
    $width = $canvas->get_width();
    $height = $canvas->get_height();

    //Especifica a Imagem de Marca D'Água
    $imageURL = 'img/forper.png';
    $imgHeight = 511;
    $imgWidth = 250;

    //Define a Opacidade da Imagem
    $canvas->set_opacity(.3);

    //Especifica as posições horizontal e vertical
    $x = (($width - $imgWidth) / 2);
    $y = (($height - $imgHeight) / 2);

    //Adiciona a Imagem ao PDF
    $canvas->image($imageURL, $x, $y, $imgWidth, $imgHeight);

    //Exibibir a página
    $dompdf->stream(
        "$nip.pdf", 
        array(
            'Attachment' => false //Para realizar o download somente alterar para true
        )
    );
?>

我更改了 HTML,因为旧的太大了。 如果只有一页,水印会正确显示,但如果有两页或更多页,图像只会出现在最后一页。

我尝试添加下面的代码以在第一页添加水印,但图像位于文本前面。

// Dirty fix for images and lines
    $canvas->page_script('
        $image = \'img/image.png\';
        // $pdf is the variable containing a reference to the canvas object provided by dompdf
        $pdf->image($image, ' . $x . ', ' . $y . ', ' . $imgWidth . ', ' . $imgHeight . ');
    '); 
php dompdf
2个回答
0
投票

您必须使用 page_script 函数(就像您所做的那样),但您也需要编辑内部的画布。 为了避免格式错误并保持良好的维护语法,我建议仅使用画布代码添加一个新文件。

canvasScript.php

$canvas = $dompdf->getCanvas();

$width = $canvas->get_width();
$height = $canvas->get_height();

$imageURL = 'img/forper.png';
$imgHeight = 511;
$imgWidth = 250;

$canvas->set_opacity(.3);

$x = (($width - $imgWidth) / 2);
$y = (($height - $imgHeight) / 2);

$canvas->image($imageURL, $x, $y, $imgWidth, $imgHeight);

您的代码

// [...]
// Renderize HTML
$dompdf->render();

// Add watermark to all pages
$canvasScript = file_get_contents('/canvasScript.php');
$canvas->page_script($canvasScript); 

$dompdf->stream("your_pdf.pdf");

dompdf没有文档,缺乏这种用法,我建议下次使用mpdf/mpdf


0
投票

通过设置水印插入前和插入后图像的不透明度找到了解决方案。

    $pdf = Pdf::loadView($view, $data); 
    $pdf->render();
    $canvas = $pdf->getCanvas();
    $canvas->page_script(
        function ($pageNumber,$pageCount,$canvas,$fontMetrics){    
        
        $w = $canvas->get_width(); 
        $h = $canvas->get_height();            
        $imageURL = '';//path to image
        $imgWidth = 500; 
        $imgHeight = 400; 
        
        // Set opacity for current image
        $canvas->set_opacity(.2); 
        
        
        $x = (($w-$imgWidth)/2); 
        $y = (($h-$imgHeight)/2); 
        
         
        $canvas->image($imageURL, $x, $y, $imgWidth, $imgHeight);
        //reset opacity (this does the trick)
        $canvas->set_opacity(1); 
    
    });
    return $pdf->stream('pdf.pdf',array("Attachment" => 0));
© www.soinside.com 2019 - 2024. All rights reserved.