Free Support Forum - aspose.cloud

Add Stamp to PDF in Node.js using Google Drive Storage with Aspose.PDF REST API produces no results

When using the Node.js SDK to add a Text Stamp that is utilizing Google Drive storage, I get a success 200 but the file in Google Drive remains unchanged and I do not receive a stream of data from the response. I may have the sequence/ordering of how this works i.e. can you add a stamp to Google Drive storage, or do I need to temporarily move it to internal storage for the processing?

const { PdfApi } = require("asposepdfcloud");
const pdfApi = new PdfApi(process.env.ASPOSE_CLIENT_ID, process.env.ASPOSE_CLIENT_SECRET);
let pdfDocName = "LD_FP_CORE 01_MAIN1 (1).pdf";
let remoteFolder = "Aspose Demo";
const stamp = new TextStamp();
...

pdfApi.postPageTextStamps(pdfDocName, 1, [stamp], null, remoteFolder).then((result) => {
            console.log(result.response);
            console.log(result.body);

            // I DO NOT HAVE A WRITABLE STREAM/BUFFER HERE
            // FILE IN STORAGE IS UNCHANGED
            // HOW DO I RECEIVE THE DOCUMENT WITH THE PDF?  
            // I CAN USE google.drive.create IF NEEDED, BUT 
            // HOW/WHERE DO I GET THE MODIFIED PDF WITH THE STAMP?
});

@ppayneliq

I tried following sample code with my google drive storage, configured in aspose.cloud dashboard, without any issue. Please find the sample output. line.pdf (34.7 KB)

Please make sure you are passing the correct storage name in the API call. However, if you still face the issue then please share our working code along with your credentials in a private message. We will further investigate it.


const { PdfApi } = require("asposepdfcloud");
const { TextStamp }= require("asposepdfcloud/src/models/textStamp");
const { TextState }= require("asposepdfcloud/src/models/textState");
const { HorizontalAlignment }= require("asposepdfcloud/src/models/horizontalAlignment");
const { VerticalAlignment }= require("asposepdfcloud/src/models/verticalAlignment");
const { Rotation }= require("asposepdfcloud/src/models/rotation");


// Get App Key and App SID from https://dashboard.aspose.cloud/
pdfApi = new PdfApi("xxxxx-xxxx-xxxx-xxxx-xxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxx");
var fs = require('fs');

const name = "line.pdf";
const pageNumber = 1;
//const remoteTempFolder = "Temp";
const storage="myGoogleAspose";

// Add Text Stamp
const textState = new TextState();
textState.fontSize = 14;
textState.font = 'Arial';

const stamp = new TextStamp();
stamp.background = true;
stamp.leftMargin = 1;
stamp.rightMargin = 2;
stamp.topMargin = 3;
stamp.bottomMargin = 4;
stamp.horizontalAlignment = HorizontalAlignment.Center;
stamp.verticalAlignment = VerticalAlignment.Center;
stamp.opacity = 1;
stamp.rotate = Rotation.None;
stamp.rotateAngle = 0;
stamp.xIndent = 0;
stamp.yIndent = 0;
stamp.zoom = 1;
stamp.textAlignment = HorizontalAlignment.Center;
stamp.value = "Aspose.PDF Cloud";
stamp.textState = textState;

pdfApi.postPageTextStamps(name, pageNumber,[stamp], storage, null).then((result) => {    
    console.log(result.body.code);    			    
}).catch(function(err) {
    // Deal with an error
    console.log(err);
});

I think I mentioned I was able to get 200 success response code, but:

  • File on Google remains unchanged (doesn’t have a Stamp) - [THIS IS RESOLVED - I AM NOW VIEWING THE FILE REVISIONS THAT INCLUDE THE STAMP]
  • The result.response and the result.body is not a Buffer/Stream to be able to save a resulting document. This is still not working. Does the response (or another API call) exist to obtain the resulting Document to save as a “separate” file, like the below:

For Exmaple:
pdfApi.postPageTextStamps(pdfDocName, 1, [stamp], null, remoteFolder).then((result) => {
if (result.body.code == ‘200’) {
drive.files.create({
auth: auth,
resource: {
name: “LD_FP_CORE 01_MAIN1 (1)_Watermarked.pdf”,
parents : [‘1oR7G-648QO3QqsMO5dQaf8l6XaBwMO_9’],
mimeType: “application/pdf”,
},
media: {
mimeType: “application/pdf”,
body: result.body
}
}).then(fileData => {
console.log(fileData.data.id);
res.json({status : ‘success’});
});
}
}).catch(function(err) {
// Deal with an error
console.log(err);
});

Produces the following error:
TypeError: part.body.pipe is not a function

  • or -
    var writeStream = fs.createWriteStream(’/Users/paulpayne/Desktop/velocityS3/SFDC Projects/akg-service-ticket/tmp/new.pdf’);
    writeStream.write(result.body);

Response:
TypeError [ERR_INVALID_ARG_TYPE]: The “chunk” argument must be one of type string or Buffer. Received type object
at validChunk (_stream_writable.js:272:10)
at WriteStream.Writable.write (_stream_writable.js:307:21)
at /Users/paulpayne/Desktop/velocityS3/SFDC Projects/akg-service-ticket/server.js:318:16
at processTicksAndRejections (internal/process/task_queues.js:94:5) {
code: ‘ERR_INVALID_ARG_TYPE’

@ppayneliq

The PostPageTextStamps API does not return the output document in response but saves on the storage. You need to download a file from storage using DownloadFile API as following. Hopefully, it will help you to accomplish the task.

const name = "dummy.pdf";
const remoteTempFolder = "Temp";
const path = remoteTempFolder + "\\" + name;

//Download file
const localPath = "C:/Temp/dummy_textstamp.pdf";

pdfApi.downloadFile(path).then((result) => {    
	fs.writeFileSync(localPath, result.body);
    console.log("File Downloaded");    
}).catch(function(err) {
    // Deal with an error
    console.log(err);
});

Ok, that makes sense. My solution then will be to use the copyFile API, followed by my transformations, or better use the built-in versioning of Google Drive. Thanks for your help!

1 Like