Monday, March 11, 2013

Search & Replace Text inside PDF Files in PHP Applications

This technical tip allows developers to replace text in a PDF file using Saaspose Pdf REST API in your PHP applications. Saaspose.Pdf also converts PDF file to DOC, DOCX, HTML, XPS, TIFF etc. You can create a new PDF either from scratch or from HTML, XML, template, database, XPS or an image.  Some important steps for performing this task are, enter App SID and App key, build URI, browse Pdf file, enter old text, enter new text, Build URI to replace text, Save PDF file on server, Sign remote URI, Send request and get status in the response, Specify remote URI to get replace text in PDF file and Save output stream to disk.
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... <br/>";
    $strURIRequest = "http://api.saaspose.com/v1.0" . "/storage/file/" . $fileName;
    $signedURI = Sign($strURIRequest);
    uploadFileBinary($signedURI, $filePath);
    echo "Pdf file has been uploaded successully <br/>";
    echo "Replacing text...<br/>";
    //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
       $strURI = "http://api.saaspose.com/v1.0" . "/storage/file/" . $fileName;
       //sign URI
       $signedURI = Sign($strURI);
       $responseStream = processCommand($signedURI, "GET", "", "");
       $outputPath = getcwd() . "\\output\\" . $fileName;
       saveFile($responseStream, $outputPath);
       echo "The text has been replaced and Pdf file has saved at: " . $outputPath;
    }
    else
       return $v_output;
About Saaspose
SaaSpose is a cloud-based document generation, conversion and automation platform for developers. Using SaaSpose makes it easy for Web & Mobile Developers to work with Microsoft Word documents, Microsoft Excel spreadsheets, Microsoft PowerPoint presentations, Adobe PDFs, OpenDocument formats, and email formats and protocols in their Apps.  The SaaSpose REST API enables you to quickly integrate the following into your Web: Document Assembly & Mail-Merge, Reporting, Document Conversion, Text and Image Extraction, Device Targeting, Metadata Removal, Barcode Recognition, Generation & Embedding, Email Templating & Tracking. The REST API can be called from any platform: .NET, Java, Ruby, Salesforce, Amazon etc.
More about Saaspose.Pdf
Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
Saaspose - Your File Format Experts 2.0
sales@aspose.com
Phone: 1.214.329.1520
Fax: 866.810.9465

No comments:

Post a Comment