When trying to use the PHP API to replace a text in a PDF document, I get this error:
Aspose\PDF\ApiException
[400] Client error:
`POST https://api.aspose.cloud/v3.0/pdf/kk-mappe.pdf/text/replace`
resulted in a `400 Bad Request`
response: {"TextReplaces":["The TextReplaces field is required."]}
My code looks like this:
<?php
namespace App\Http\Controllers;
use Aspose\PDF\Api\PdfApi;
use Illuminate\Http\Request;
use Aspose\PDF\Configuration;
use Aspose\PDF\Model\TextReplace;
use Aspose\PDF\Model\TextReplaceListRequest;
class AsposePDFController extends Controller {
private $baseURL = "https://api.aspose.cloud/v3.0/";
private function getAPI() {
$appSid = 'XXX';
$appKey = 'XXX';
$config = new Configuration();
$config->setAppKey($appKey);
$config->setAppSid($appSid);
return new PdfApi(null, $config);
}
public function replacePDFText(Request $request) {
$api = $this->getAPI();
$sourcePath = "C:/xampp7_4/htdocs/kkbackend/public/uploads/documents/";
$sourceFile = $sourcePath . "kk-mappe.pdf";
$remoteFileName = "kk-mappe.pdf";
$outputFile = $sourcePath . "kk-mappe-replaced.pdf";
// Upload File
$api->uploadFile(
$remoteFileName,
$sourceFile
);
// Replace the Text
$textReplace = new TextReplace();
$textReplace->old_value = "Iserlohn";
$textReplace->new_value = "Crailsheim";
$textReplace->regex = false;
$textReplaceRequest = new TextReplaceListRequest();
$textReplaceRequest->text_replaces = [$textReplace];
// Call the TextReplace API
// THIS IS WHERE THE ERROR OCCURS
$response = $api->postDocumentTextReplace(
$remoteFileName,
$textReplaceRequest,
null, // Storage
null // Folder
);
// Download the resulting file
$downloadResult = $api->downloadFile($remoteFileName);
// Save it
copy($downloadResult->getPathName(), $outputFile);
return $downloadResult;
}
}
What am I missing?
Thanks a lot for help or a working example to replace text in a PDF document using the PHP API.
Daniel