{"id":280,"date":"2022-01-24T18:41:53","date_gmt":"2022-01-24T18:41:53","guid":{"rendered":"https:\/\/mouryasolutions.in\/blog\/?p=280"},"modified":"2023-01-29T19:32:24","modified_gmt":"2023-01-29T19:32:24","slug":"watermark-on-pdf-using-laravel","status":"publish","type":"post","link":"https:\/\/mouryasolutions.in\/blog\/watermark-on-pdf-using-laravel\/","title":{"rendered":"How to apply watermark on pdf using Laravel"},"content":{"rendered":"<div class=\"post-content\">\r\n<blockquote class=\"wp-block-quote\">\r\n<p>You can apply a watermark on a PDF file using the package &#8220;setasign\/fpdi-fpdf&#8221; in Laravel. Here are the steps:<\/p>\r\n<\/blockquote>\r\n\r\n\r\n\r\n<h4>Install the package:<\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>composer require setasign\/fpdi-fpdf<\/code><\/pre>\r\n\r\n\r\n\r\n<h4>Create a new class for watermarking:<\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>use FPDI;\r\n\r\nclass WatermarkPdf extends FPDI {\r\n    public function Header() {}\r\n    public function Footer() {}\r\n    public function Watermark($file, $text) {\r\n        \/\/ Set alpha to semi-transparency\r\n        $this-&gt;SetAlpha(0.5);\r\n        \/\/ Get the page width\/height\r\n        $ws = $this-&gt;getPageWidth();\r\n        $hs = $this-&gt;getPageHeight();\r\n        \/\/ Print the watermark\r\n        if ($file) {\r\n            $this-&gt;Image($file, 0, 0, $ws, $hs);\r\n        }\r\n        if ($text) {\r\n            $this-&gt;SetFont('Arial', 'B', 50);\r\n            $this-&gt;Rotate(45, $ws\/2, $hs\/2);\r\n            $this-&gt;Text($ws\/2, $hs\/2, $text);\r\n        }\r\n        \/\/ Reset the transparency\r\n        $this-&gt;SetAlpha(1);\r\n    }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<h4>Use the class to watermark the PDF:<\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>use WatermarkPdf;\r\n\r\n$pdf = new WatermarkPdf();\r\n$pdf-&gt;AddPage();\r\n$pdf-&gt;setSourceFile('existing.pdf');\r\n$pdf-&gt;Watermark('watermark.png', 'WATERMARK');\r\n$pdf-&gt;Output('watermarked.pdf', 'F');\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Note: You can use either an image file or a text as a watermark.<\/p>\r\n<\/div>","protected":false},"excerpt":{"rendered":"<p>You can apply a watermark on a PDF file using<a href=\"https:\/\/mouryasolutions.in\/blog\/watermark-on-pdf-using-laravel\/\">Read More<i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":508,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/280"}],"collection":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=280"}],"version-history":[{"count":8,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/280\/revisions"}],"predecessor-version":[{"id":461,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/280\/revisions\/461"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media\/508"}],"wp:attachment":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}