PHP - Add watermark to a DOC or PDF

Asked

Viewed 1,486 times

2

I need you to take my document . doc or . pdf and add a watermark D`, it can also be the mark "DRAFT" when viewing.

The BO is as follows, need to view a document but need to have something identifying that this document is not official, ie with some watermark or some information in the header of the page written "draft, uncontrolled copy..."

Thanks in advance!

  • 1

    Show us the header you already have so we can help you better.

  • hello, the header was an example, what I need is to put a message in the document, I have the document saved . doc or . pdf, now I need to add the watermark, I saw some things about Phpword, however I only found to generate the document and not how to add in the existing document.

1 answer

3


It seems that it is possible without big problems...

For PDF, you have the library PDF Watermarker. Example of her use:

<?php
require_once('pdfwatermarker/pdfwatermarker.php');
require_once('pdfwatermarker/pdfwatermark.php');

//Specify path to image. The image must have a 96 DPI resolution.
$watermark = new PDFWatermark('C:\myimage.png'); 

//Set the position
$watermark->setPosition('bottomleft');

//Place watermark behind original PDF content. Default behavior places it over the content.
$watermark->setAsBackground();

//Specify the path to the existing pdf, the path to the new pdf file, and the watermark object
$watermarker = new PDFWatermarker('C:\test.pdf','C:\output.pdf',$watermark); 

//Set page range. Use 1-based index.
$watermarker->setPageRange(1,5);

//Save the new PDF to its specified location
$watermarker->savePdf(); 
?>

For Word, I found it on Github using the library you mentioned, Phpword. Except you didn’t open any files to edit, I don’t know if that would be hard:

<?php
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Create header
$header = $section->createHeader();
// Add a watermark to the header
$header->addWatermark('_earth.jpg', array('marginTop'=>200, 'marginLeft'=>55));
$section->addText('The header reference to the current section includes a watermark image.');
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Watermark.docx');
?>

Sources:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.