How To Compare Two PDF Documents in PHP With GroupDocs.Comparison REST API?

Hello,
I want to use cURL with PHP to compare two PDF files.
I’m passing two PDF files with credentials (Bearer in header request) but it shows me error 400 for format not supported : File of this format doesn’t support comparison. Perhaps you have specified an invalid file extension or the file is damaged.
When it’s not this message, i have this return : CloudFront attempted to establish a connection with the origin, but either the attempt failed or the origin closed the connection.
We can’t connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.

Can you help me please ?

@nathaniel.merino

I have tested the comparison API with sample PDF documents and unable to notice any issue. Please share your sample code along with the source documents. We will investigate the issue and will guide you.

Here is the code :

// Set cURL parameters
$url = ‘https://api.groupdocs.cloud/v2.0/comparison/comparisons’;
$headers = [
‘accept: application/json’,
‘Content-Type: application/json’,
‘x-aspose-client: Containerize.Swagger’,
'authorization: Bearer ’ . $this->accessToken
];
$data = json_encode([
‘SourceFile’ => [
‘FilePath’ => $pathSource,
// ‘VersionId’ => null,
// ‘StorageName’ => null,
// ‘Password’ => null
],
‘TargetFiles’ => [
[
‘FilePath’ => $pathTarget,
// ‘VersionId’ => null,
// ‘StorageName’ => null,
// ‘Password’ => null
]
],
‘Settings’ => [
‘GenerateSummaryPage’ => true,
‘ShowDeletedContent’ => true,
‘ShowInsertedContent’ => true,
‘StyleChangeDetection’ => true,
// ‘InsertedItemsStyle’ => [
// ‘FontColor’ => ‘string’,
// ‘HighlightColor’ => ‘string’,
// ‘BeginSeparatorString’ => ‘string’,
// ‘EndSeparatorString’ => ‘string’,
// ‘Bold’ => true,
// ‘Italic’ => true,
// ‘StrikeThrough’ => true,
// ‘Underline’ => true
// ],
// ‘DeletedItemsStyle’ => [
// ‘FontColor’ => ‘string’,
// ‘HighlightColor’ => ‘string’,
// ‘BeginSeparatorString’ => ‘string’,
// ‘EndSeparatorString’ => ‘string’,
// ‘Bold’ => true,
// ‘Italic’ => true,
// ‘StrikeThrough’ => true,
// ‘Underline’ => true
// ],
// ‘ChangedItemsStyle’ => [
// ‘FontColor’ => ‘string’,
// ‘HighlightColor’ => ‘string’,
// ‘BeginSeparatorString’ => ‘string’,
// ‘EndSeparatorString’ => ‘string’,
// ‘Bold’ => true,
// ‘Italic’ => true,
// ‘StrikeThrough’ => true,
// ‘Underline’ => true
// ],
// ‘WordsSeparatorChars’ => [
// ‘string’
// ],
// ‘UseFramesForDelInsElements’ => true,
// ‘CalculateComponentCoordinates’ => true,
// ‘MarkChangedContent’ => true,
// ‘MarkNestedContent’ => true,
// ‘MetaData’ => [
// ‘Author’ => ‘string’,
// ‘LastSaveBy’ => ‘string’,
// ‘Company’ => ‘string’
// ],
// ‘Password’ => ‘string’,
// ‘DiagramMasterSetting’ => [
// ‘MasterPath’ => ‘string’,
// ‘UseSourceMaster’ => true
// ],
// ‘OriginalSize’ => [
// ‘Width’ => 0,
// ‘Height’ => 0
// ],
‘HeaderFootersComparison’ => true,
// ‘SensitivityOfComparison’ => 0
],
‘OutputPath’ => $pathComparison
]);


        // Set cURL and call
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        $json_response = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $errors = curl_error($curl);
        curl_close($curl);

And files :
Billet Maxime.pdf (141.4 KB)
Billet Maxime.pdf (141.4 KB)
(The files are the same)

@NathanielMerino

Please confirm are you passing the same file path for source and target file? As I notice if I use the same file path as a source and target file then I get your above reported error. Otherwise, it works fine. Secondly, you need to generate JSON Web Token for request authentication.

    $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://api.groupdocs.cloud/connect/token');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials&client_id=$AppSid&client_secret=$AppKey");
	curl_setopt($ch, CURLOPT_POST, 1);
	
	$headers = array();
	$headers[] = 'Content-Type: application/x-www-form-urlencoded';
	$headers[] = 'Accept: application/json';
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	
	$result = curl_exec($ch);
	//print_r($result);
	if (curl_errno($ch)) 
	{
		echo 'Error:' . curl_error($ch);
	}
	else
	{
		$resultfin = json_decode($result,true);
		$access_token = $resultfin['access_token'];
		
	}
	curl_close($ch);

I generate JSON Web Token before call API.
I don’t provide the same paths for source and target when calling, there are two separate files :image.png (24.2 KB)

Here are the values provided:
{"SourceFile":{"FilePath":"\/comparison\/sources\/d8dc84ac4bdcc7dab418dac479e7f75fce4701973f029c20f2947602c87f93be.pdf"},"TargetFiles":[{"FilePath":"\/comparison\/sources\/c82cde6dc80792a727b43b515f02b033605121033f8dbc96a3f9adfb17583681.pdf"}],"Settings":{"GenerateSummaryPage":true,"ShowDeletedContent":true,"ShowInsertedContent":true,"StyleChangeDetection":true,"HeaderFootersComparison":true},"OutputPath":"\/comparison\/results\/ee1015d4ad789058e87c42a5a3d8332c51983dee6fe29176cce5bf74a9b4bef1.pdf"}

And the response :
{"error":{"code":"error","message":"File of this format doesn't support comparison. Perhaps you have specified an invalid file extension or the file is damaged.","description":"Operation Failed.","innerError":{"requestId":null,"date":"2020-10-30T16:25:18.0054653Z"}}}

@NathanielMerino

Thanks for your feedback. I tried your sample PHP code to compare two PDF documents and it is working fine on my end. Are you getting the error message for some specific files or all the files? As the above error looks like file specific issue. Can you please share above mentioned PDF files as well?

Compare Two PDF Documents in PHP

<?php

	$url = 'https://api.groupdocs.cloud/v2.0/comparison/comparisons';
$headers = [
'accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer eyJhb......ZtBBcuoC5ahrXA'
	];
$data = json_encode([
'SourceFile' => [
'FilePath' => 'pdf/d8dc84ac4bdcc7dab418dac479e7f75fce4701973f029c20f2947602c87f93be.pdf',
// 'VersionId' => null,
// 'StorageName' => null,
// 'Password' => null
],
'TargetFiles' => [
[
'FilePath' => 'pdf/c82cde6dc80792a727b43b515f02b033605121033f8dbc96a3f9adfb17583681.pdf',
// 'VersionId' => null,
// 'StorageName' => null,
// 'Password' => null
]
],
'Settings' => [
'GenerateSummaryPage' => true,
'ShowDeletedContent' => true,
'ShowInsertedContent' => true,
'StyleChangeDetection' => true,
// 'InsertedItemsStyle' => [
// 'FontColor' => 'string',
// 'HighlightColor' => 'string',
// 'BeginSeparatorString' => 'string',
// 'EndSeparatorString' => 'string',
// 'Bold' => true,
// 'Italic' => true,
// 'StrikeThrough' => true,
// 'Underline' => true
// ],
// 'DeletedItemsStyle' => [
// 'FontColor' => 'string',
// 'HighlightColor' => 'string',
// 'BeginSeparatorString' => 'string',
// 'EndSeparatorString' => 'string',
// 'Bold' => true,
// 'Italic' => true,
// 'StrikeThrough' => true,
// 'Underline' => true
// ],
// 'ChangedItemsStyle' => [
// 'FontColor' => 'string',
// 'HighlightColor' => 'string',
// 'BeginSeparatorString' => 'string',
// 'EndSeparatorString' => 'string',
// 'Bold' => true,
// 'Italic' => true,
// 'StrikeThrough' => true,
// 'Underline' => true
// ],
// 'WordsSeparatorChars' => [
// 'string'
// ],
// 'UseFramesForDelInsElements' => true,
// 'CalculateComponentCoordinates' => true,
// 'MarkChangedContent' => true,
// 'MarkNestedContent' => true,
// 'MetaData' => [
// 'Author' => 'string',
// 'LastSaveBy' => 'string',
// 'Company' => 'string'
// ],
// 'Password' => 'string',
// 'DiagramMasterSetting' => [
// 'MasterPath' => 'string',
// 'UseSourceMaster' => true
// ],
// 'OriginalSize' => [
// 'Width' => 0,
// 'Height' => 0
// ],
'HeaderFootersComparison' => true,
// 'SensitivityOfComparison' => 0
],
'OutputPath' => 'pdf/output.pdf'
]);

        // Set cURL and call
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        $json_response = curl_exec($curl);
		print_r($json_response);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $errors = curl_error($curl);
        curl_close($curl);
	
?>

Here is mentioned PDF files : files.zip (272.1 KB)

I did another try with another file: Bordereaux-affranchissement.pdf (88.9 KB)
And the response is the following :
>

    <HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <TITLE>ERROR: The request could not be satisfied</TITLE>
    </HEAD><BODY>
    <H1>504 ERROR</H1>
    <H2>The request could not be satisfied.</H2>
    <HR noshade size="1px">
    CloudFront attempted to establish a connection with the origin, but either the attempt failed or the origin closed the connection.
    We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
    <BR clear="all">
    If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
    <BR clear="all">
    <HR noshade size="1px">
    <PRE>
    Generated by cloudfront (CloudFront)
    Request ID: 8gVDNU0xcd6n_eaxb1DR6fUjNmoZsfDk7wLodfBoPAHMbau2L0KHhA==
    </PRE>
    <ADDRESS>
    </ADDRESS>
    </BODY></HTML>  
    [2020-10-30 17:23:02] local.ERROR: /media/sf_oqto/app/ExternalServices/GroupDocsService.php(line 413) : Status 504 - Erreur lors de la comparaison - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <TITLE>ERROR: The request could not be satisfied</TITLE>
    </HEAD><BODY>
    <H1>504 ERROR</H1>
    <H2>The request could not be satisfied.</H2>
    <HR noshade size="1px">
    CloudFront attempted to establish a connection with the origin, but either the attempt failed or the origin closed the connection.
    We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
    <BR clear="all">
    If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
    <BR clear="all">
    <HR noshade size="1px">
    <PRE>
    Generated by cloudfront (CloudFront)
    Request ID: 8gVDNU0xcd6n_eaxb1DR6fUjNmoZsfDk7wLodfBoPAHMbau2L0KHhA==
    </PRE>
    <ADDRESS>
    </ADDRESS>
    </BODY></HTML>

@NathanielMerino

Thanks for sharing the input files. I have tested again the scenario and getting your above reported exception randomly. So logged a ticket COMPARISONCLOUD-157 for further investigation and rectification. We will update you as soon as it is resolved.

Hi, any news?

@NathanielMerino

We have investigated your shared PDF documents, but it seems your documents have the wrong PDF signature. These are not detected as a PDF, which is causing the issue.