mPDF Manual – mPDF functions

OverWrite()

(mPDF ≥ 2.3)

OverWrite – Replace specified text strings in an existing PDF file

Description

mixed OverWrite ( string $sourcefile , mixed $search , mixed $replacement [, string $dest [, string $file_out ]])

Using the class mPDF, (until mPDF 8.0 after enabling with SetImportUse), an existing PDF file can be overwritten, replacing specified text with alternatives. For example you may have created a long complex PDF file, and you wish to produce copies with an individual number on each copy without having to re-generate the whole document each time.

Overwrite() does not re-flow the text from the source file. If the $replacement string is longer than the $search string, it may overlap the following text.

Parameters

$sourcefile

This parameter specifies the source PDF file to use. $sourcefile should be a relative path to a local file.

$search

The pattern to search for. It can be either a string or an array with strings. Must only contain only ASCII characters.

If the document is utf-8 mode, the search patterns must not exist in text with justified alignment. (Justified text is achieved in mPDF by varying the character spacing for each SPACE between words; this breaks up the text in the PDF file.)

$replacement

The string or an array with strings to replace. $replacement can contain any utf-8 encoded characters.

If this parameter is a string and the $search parameter is an array, only the first $search element will be replaced by the $replacement string, any extra $search s will be replaced by an empty string. If both $search and $replacement parameters are arrays, each $search will be replaced by the $replacement counterpart. If there are fewer elements in the $replacement array than in the $search array, any extra $search s will be replaced by an empty string.

$dest

$dest specifies the destination for the generated PDF document.

Default: "D"

Values

  • 'D': download the PDF file
  • 'I': serves in-line to the browser
  • 'S': returns the PDF document as a string
  • 'F': save as file $file_out
$file_out

This parameter specifies the filename for the output PDF file. No path should be included unless $dest is set as "F".

Default: "mpdf.pdf"

Return Value

OverWrite() returns the PDF file as a string if $dest is set to "S".

Changelog

Version Description
2.3 Function was added.

Examples

Example #1

<?php

// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';

// Must set codepage (e.g. UTF-8 or Core fonts) the same as for original document
// The rest of the parameters do nothing
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetImportUse(); // only with mPDF <8.0

// forces no subsetting - otherwise the inserted characters may not be contained
// in a subset font
$mpdf->percentSubset = 0;

$search = array(
	'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
	'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZ'
);

$replacement = array(
	"personalised for Jos\xc3\xa9 Bloggs",
	"COPYRIGHT: Licensed to Jos\xc3\xa9 Bloggs"
);

$mpdf->OverWrite('test.pdf', $search, $replacement, 'I', 'mpdf.pdf' ) ;

Example #2 Using encryption

<?php

// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->percentSubset = 0;
$mpdf->SetProtection(array(), '', 'bread');   // Need to specify a password

$mpdf->WriteHTML('This copy is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$mpdf->Output('test.pdf','F');

// Have to save various encryption keys, which are uniquely generated each document
$uid = $mpdf->uniqid;
$oval = $mpdf->Ovalue;
$encKey = $mpdf->encryption_key;
$uval = $mpdf->Uvalue;
$pval = $mpdf->Pvalue;
$RC128 = $mpdf->useRC128encryption;

unset($mpdf);

//==============================================================

$mpdf = new \Mpdf\Mpdf();
$mpdf->SetImportUse(); // only with mPDF <8.0

// Re-instate saved encryption keys from original document
$mpdf->encrypted = true;
$mpdf->useRC128encryption = $RC128;
$mpdf->uniqid = $uid ;
$mpdf->Ovalue = $oval ;
$mpdf->encryption_key = $encKey ;
$mpdf->Uvalue = $uval ;
$mpdf->Pvalue = $pval ;

$search = array(
	'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
);

$replacement = array(
	"personalised for Jos\xc3\xa9 Bloggs"
);

$mpdf->OverWrite('test.pdf', $search, $replacement, 'I', 'mpdf.pdf' ) ;

See Also

Fork me on GitHub