【问题标题】:How to add watermark in pdf using nodejs?如何使用nodejs在pdf中添加水印?
【发布时间】:2018-08-29 10:30:54
【问题描述】:

我通过nodejs生成了pdf。我想在这个生成的 pdf 中添加水印。我在代码中使用了 dynamic-html-pdf 插件。如果有在 dynamic-html-pdf 中添加水印的任何选项。我在这里展示我的示例代码。

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 computer is a device that can be instructed to carry out sequences of arithmetic or logical operations automatically via computer 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'
    });

});

【问题讨论】:

    标签: javascript node.js node-html-pdf


    【解决方案1】:

    保存您的 pdf 文档后。使用 image-watermark 模块将水印附加到您生成的 pdf 中。

    var watermark = require('image-watermark'); 
    watermark.embedWatermark('/path/to/your/generated/pdf', {'text' : 'sample watermark'});
    

    【讨论】:

    • /usr/local/lib/node_modules/image-watermark/watermark.js:196 throw new Error('Image-Watermark::embedWatermark : Unable to process image file : ' + err);
    • 它仅适用于图像文件。当我将 pdf 作为输入时,它会显示上述错误
    【解决方案2】:

    在PDF文档中添加文本水印的另一种解决方案是Aspose.PDF Cloud SDK for Node.js。它是一个商业产品,但每月提供 150 次免费 API 调用。

    目前,它支持来自云存储的 PDF 文件处理:Aspose 内部存储、Amazon S3、DropBox、Google Drive Storage、Google Cloud Storage、Windows Azure Storage 和 FTP Storage。但是,我们计划添加支持以处理来自请求正文(流)的 PDF 文档。

    P.S:我是 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);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 2018-11-11
      相关资源
      最近更新 更多