PHP - Add watermark in a DOC or PDF

2

I need you to get my .doc or .pdf document and add a Watermark, it can also be the "DRAFT" tag when you are viewing.

The BO is the following, I need to visualize a document but it needs to have something identifying that this document is not official, that is with some water mark or some information in the header of the page written "draft, uncontrolled copy ... "

Thank you in advance!

    
asked by anonymous 06.10.2017 / 16:51

1 answer

2

It seems like it's possible without major problems ...

For PDF, you have the PDF Watermarker library. Example usage:

<?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 in Github using the library you mentioned, PHPWord. But it did not open any files to edit, I do not know if this would be difficult:

<?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:

06.10.2017 / 17:24