PHP Code Sample to Replace Multiple Text in PDF





1
Date Submitted Thu. May. 8th, 2014 6:26 AM
Revision 1 of 1
Helper johansonkatherine
Tags in | inside | pdf | PHP | Replace | Text
Comments 0 comments
The following code sample shows how developers can replace multiple texts PDF file in php using Aspose.Pdf for Cloud API in their applications. Developers can use Aspose REST API with any language: .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more.


use Aspose\Cloud\Common\AsposeApp;
use Aspose\Cloud\Common\Utils;
use Aspose\Cloud\Common\Product;
                                               
AsposeApp::$appSID = "77******-1***-4***-a***-80**********";
AsposeApp::$appKey = "********************************";

$filePath = getcwd() . "/Input/test.pdf";
$fileName = basename($filePath);
$oldText1 = "[!firm!]";
$newText1 = "Aspose";
$oldText2 = "[!client!]";
$newText2 = "Mark";
$oldText3 = "[!transaction_date!]";
$newText3 = "01-01-2014";

//build URI
echo "Uploading pdf file... <br/>";
$strURIRequest = "http://api.aspose.com/v1.1/storage/file/" . $fileName;
$signedURI = Utils::sign($strURIRequest);

echoUtils::uploadFileBinary($signedURI, $filePath);
echo "Pdf file has been uploaded successully<br/>";

echo "Replacing text...<br/>";
//Build JSON to post
$fieldsArray = array('TextReplaces'=>array(array('OldValue'=>$oldText1, 'NewValue'=>$newText1, 'Regex'=>'false'),
        array('OldValue'=>$oldText2, 'NewValue'=>$newText2, 'Regex'=>'false'),
        array('OldValue'=>$oldText3, 'NewValue'=>$newText3, 'Regex'=>'false')));
$json = json_encode($fieldsArray);

//Build URI to replace text
$strURI = "http://api.aspose.com/v1.1/pdf/" . $fileName . "/replaceTextList";
$signedURI = Utils::sign($strURI);

$responseStream = Utils::processCommand($signedURI, "POST", "json", $json);

//Save PDF file on server
//build URI
$strURI = "http://api.aspose.com/v1.1/storage/file/" . $fileName;
//sign URI
$signedURI = Utils::sign($strURI);
$responseStream = Utils::processCommand($signedURI, "GET", "", "");
$outputPath = getcwd() . "/output/" . $fileName;
Utils::saveFile($responseStream, $outputPath);
echo "The text has been replaced and Pdf file has saved at: " . $outputPath;


 

Katherine Johanson

Comments

There are currently no comments for this snippet.

Voting

Votes Down