Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This technical tip allows developers to replace text in a PDF file using Saaspose Pdf REST API in your PHP applications.
Sample Code for Replacing Text in a PDF File
$filePath = getcwd() . "\\Input\\MyFile.pdf";
$fileName = basename($filePath);
$oldText = "old text here";
$newText = "new text here";
//set application information
$AppSID = "77**************";
$AppKey = "89***********";
//build URI
echo "Uploading pdf file... ";
$strURIRequest = "http://api.saaspose.com/v1.0" . "/storage/file/" . $fileName;
$signedURI = Sign($strURIRequest);
uploadFileBinary($signedURI, $filePath);
echo "Pdf file has been uploaded successully ";
echo "Replacing text... ";
//Build JSON to post
$fieldsArray = array('OldValue'=>$oldText, 'NewValue'=>$newText, 'Regex'=>"false");
$json = json_encode($fieldsArray);
//Build URI to replace text
$strURI = "http://api.saaspose.com/v1.0" . "/pdf/" . $fileName . "/replaceText";
$signedURI = Sign($strURI);
$responseStream = processCommand($signedURI, "POST", "json", $json);
$v_output = ValidateOutput($responseStream);
if ($v_output === "")
{
//Save PDF file on server
//build URI