Important alert: (current site time 7/15/2013 9:48:01 AM EDT)
 

article

Search & Replace Text inside PDF Files in PHP Applications

Email
Submitted on: 3/14/2013 11:34:57 AM
By: Saaspose 
Level: Intermediate
User Rating: Unrated
Compatibility: PHP 4.0, PHP 5.0
Views: 941
(About the author)
 
     This technical tip allows developers to replace text in a PDF file using Saaspose Pdf REST API in your PHP applications.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				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... 
"; $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 $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; More about Saaspose.Pdf - Homepage of Saaspose.Pdf: http://saaspose.com/api/pdf - More Technical Tips by Saaspose.Pdf: http://saaspose.com/docs/display/pdf/1.2+-+Saaspose.Pdf+Examples


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/22/2013 9:33:37 AMRajeev

The above code give the following error :- Fatal error: Call to undefined function Sign() in E:\xampplite\htdocs\test_php\pdf.php on line 16
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.