Compare Two PDF Files for Differences in Node.js

There is an error:

let response = await compareApi.comparisons(request);
               ^^^^^

SyntaxError: await is only valid in async function

Code is here:

// initialize api

let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(

“”,

“”);

// source file

let source = new groupdocs_comparison_cloud.FileInfo();

source.filePath = “./PDF1.pdf”;

// target file

let target = new groupdocs_comparison_cloud.FileInfo();

target.filePath = “./PDF2.pdf”;

// define compare options

let options = new groupdocs_comparison_cloud.ComparisonOptions();

options.sourceFile = source;

options.targetFiles = [target];

options.outputPath = “./result.pdf”;

// create comparison request

let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);

// compare

let response = await compareApi.comparisons(request);

console.log("Output file link: " + response.href);

@ldlong

Please check the following sample code to compare two PDF files for differences in Node.js using GroupDocs.Comparison Cloud SDK for Node.js. Hopefully, it will help you to accomplish the task.

How to Compare 2 PDF Documents for Differences in Nodes.js

  1. Create a free account on groupdocs.cloud to get credentials
  2. Install GroupDocs.Comparison Cloud SDK for Node.js from NPM
  3. Create a script file and import groupdocs-comparison-cloud module
  4. Construct Comparison REST API instance
  5. Set ComparisonOptions object for the source and target PDF files
  6. Compare the PDF files using comparisons API Method

Compare Two PDF Documents for Changes Online Node.js

// load the module
var GroupDocs = require('groupdocs-comparison-cloud');

// get your Client ID and Client Secret at https://dashboard.groupdocs.cloud (free registration is required).
var clientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx";
var clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

const comparePDFFiles = async () => {

// construct ComparisonApi
var infoApi = GroupDocs.InfoApi.fromKeys(clientId, clientSecret);
var compareApi = GroupDocs.CompareApi.fromKeys(appSid, appKey);

// set the comparison options
let source = new GroupDocs.FileInfo();
source.filePath = "source_files/source.docx";
let target = new GroupDocs.FileInfo();
target.filePath = "target_files/target.docx";
let options = new GroupDocs.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "result.docx";

// compare 2 PDF documents for changes
try {
let request = new GroupDocs.ComparisonsRequest(options);

let comparePDFFilesResposne = await compareApi.comparisons(request);
} catch (err) {
throw err;
}
}

comparePDFFiles()
.then(() => {
console.log("PDF documents compared successfully");
})
.catch((err) => {
console.log("Error occurred while converting the PDF document:", err);
})