admin管理员组文章数量:1320670
I generated pdf through nodejs. i want to add watermark to this generated pdf. I used dynamic-html-pdf plugins in my code. If there is any options for adding watermark in dynamic-html-pdf. Here i display my sample code here.
var path=require('path');
var pdf = require('dynamic-html-pdf');
var html='<!DOCTYPE html><html><head><style>';
html=html+'</style>';
html=html+'</head>';
html=html+'<body>';
html=html+'<div class="divstyle1" id="content">A puter is a device that can be instructed to carry out sequences of arithmetic or logical operations automatically via puter programming. </div>';
html=html+'</body></html>';
var options = {
format: "A4",
orientation: "portrait",
border: "10mm",
base: 'file://' + path.resolve('./public/graph') + '/'
};
var document = {
type: 'file',
template: html,
context: {
img:'./public/graph/logo.jpg'
},
path: "./public/graph/mypdf.pdf"
};
pdf.create(document, options)
.then(res => {
res.status(200).json({
message: 'pdf created'
});
})
.catch(error => {
res.status(200).json({
message: 'error'
});
});
I generated pdf through nodejs. i want to add watermark to this generated pdf. I used dynamic-html-pdf plugins in my code. If there is any options for adding watermark in dynamic-html-pdf. Here i display my sample code here.
var path=require('path');
var pdf = require('dynamic-html-pdf');
var html='<!DOCTYPE html><html><head><style>';
html=html+'</style>';
html=html+'</head>';
html=html+'<body>';
html=html+'<div class="divstyle1" id="content">A puter is a device that can be instructed to carry out sequences of arithmetic or logical operations automatically via puter programming. </div>';
html=html+'</body></html>';
var options = {
format: "A4",
orientation: "portrait",
border: "10mm",
base: 'file://' + path.resolve('./public/graph') + '/'
};
var document = {
type: 'file',
template: html,
context: {
img:'./public/graph/logo.jpg'
},
path: "./public/graph/mypdf.pdf"
};
pdf.create(document, options)
.then(res => {
res.status(200).json({
message: 'pdf created'
});
})
.catch(error => {
res.status(200).json({
message: 'error'
});
});
Share
Improve this question
asked Aug 29, 2018 at 10:30
RamRam
491 gold badge1 silver badge6 bronze badges
3 Answers
Reset to default 3After saving your pdf document. Use image-watermark module to append a watermark to your generated pdf.
var watermark = require('image-watermark');
watermark.embedWatermark('/path/to/your/generated/pdf', {'text' : 'sample watermark'});
After pdf is created, use this package to watermark the pdf file. pdf-watermark
const PDFWatermark = require('pdf-watermark');
await PDFWatermark({
pdf_path: "./newsletter.pdf",
text: "Gentech", //optional
image_path: "./everest.png",
});
Another solution to add text watermark in PDF document is Aspose.PDF Cloud SDK for Node.js. It is a mercial product but provides 150 free monthly API calls.
Currently, it supports the PDF file processing from Cloud Storages: Aspose internal storage, Amazon S3, DropBox, Google Drive Storage, Google Cloud Storage, Windows Azure Storage and FTP Storage. However, we have a plan to add support to process PDF documents from the request body(stream).
P.S: I'm a developer evangelist at Aspose.
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 Client Id and Client Secret from https://dashboard.aspose.cloud/
pdfApi = new PdfApi("xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxx");
var fs = require('fs');
const name = "Test.pdf";
const pageNumber = 1;
const remoteTempFolder = "Temp";
const localTestDataFolder = "C:\\Temp";
const path = remoteTempFolder + "\\" + name;
var data = fs.readFileSync(localTestDataFolder + "\\" + name);
// Upload File
pdfApi.uploadFile(path, data).then((result) => {
console.log("Uploaded File");
}).catch(function(err) {
// Deal with an error
console.log(err);
});
// 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], null, remoteTempFolder).then((result) => {
console.log(result.body.code);
}).catch(function(err) {
// Deal with an error
console.log(err);
});
//Download file
const localPath = "C:/Temp/textstamp.pdf";
pdfApi.downloadFile(path).then((result) => {
console.log(result.response.statusCode);
console.log(result.body.byteLength);
fs.writeFileSync(localPath, result.body);
console.log("File Downloaded");
}).catch(function(err) {
// Deal with an error
console.log(err);
});
本文标签: javascriptHow to add watermark in pdf using nodejsStack Overflow
版权声明:本文标题:javascript - How to add watermark in pdf using nodejs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742062000a2418631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论