简体   繁体   English

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

[英]DOMPDF only showing image watermark on the last page

I'm trying to display an image watermark using DomPdf, but it only appears on the last page.我正在尝试使用 DomPdf 显示图像水印,但它只出现在最后一页上。 I've looked on some sites, but I haven't found anything related.我查看了一些网站,但没有找到任何相关内容。

That's my code:那是我的代码:

<?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
        )
    );
?>

I changed the HTML because the old one was too big.我换了HTML,因为旧的太大了。 If there is only one page, the watermark will appear correctly, but if there are two or more pages, the image will only appear on the last one.如果只有一页,水印会正确显示,但如果有两页或更多页,图像只会出现在最后一页。

I've tried to add the code below to add the watermark in the first page, but the image is in front of the text.我尝试添加下面的代码以在第一页中添加水印,但图像在文本前面。

// 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 . ');
    '); 

You have to use page_script function (like you did) but you need to edit the canvas inside too.您必须使用page_script function(就像您所做的那样),但您也需要在内部编辑 canvas。 To avoid formatting error and keeping a good syntax for maintaining, I suggest adding a new file with the canvas code only.为了避免格式错误并保持良好的维护语法,我建议添加一个仅包含 canvas 代码的新文件。

canvasScript.php 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);

Your code你的代码

// [...]
// 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 has no documentation and lack this kind of use, I recommend using mpdf/mpdf next time. dompdf没有文档,缺少这种用法,建议下次使用mpdf/mpdf

I ended up using mpdf as you suggested.我最终按照您的建议使用了 mpdf。 Thanks for the tip!谢谢你的提示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM