是否可以使用PHP在pdf文档中添加签名字段?我没有"数字标志"文献。我只添加一个签名字段。

时间:2015-11-12 16:16:49

标签: php pdf digital-signature

我必须生成一个PDF文档并添加一个"签名字段"。

签名字段是允许第三方服务对文档进行数字签名的字段。

是否可以使用开源解决方案在PHP中执行此操作?

我看到了https://manuals.setasign.com/setapdf-signer-manual/create-a-signature-field/

我尝试使用tcpdf,但我没有成功。

感谢。

3 个答案:

答案 0 :(得分:0)

正如您已经说过的,您已经阅读了 seta 标志库文档 here

使用 setasign,您可以创建 pdf 文档或使用现有文档在其上创建数字签名字段。下面是一个代码示例:-

<?php
require_once('library/SetaPDF/Autoload.php');

// create a writer
$writer = new SetaPDF_Core_Writer_Http('visible-field.pdf', true);
// create a new document instance
$document = new SetaPDF_Core_Document($writer);
// create at least a single page
$document->getCatalog()->getPages()->create(SetaPDF_Core_PageFormats::A4);

// add a field left top with an offset of 10 points
SetaPDF_Signer_SignatureField::add(
        $document,
        'Signature field 1',
        1,
        SetaPDF_Signer_SignatureField::POSITION_LEFT_TOP,
        array('x' => 10, 'y' => -10),
        180,
        70
 );


 // add a field absolutely position on the bottom left with an offset of 10 points
 SetaPDF_Signer_SignatureField::add($document, 'Signature field 1', 1, 10, 10, 180, 70);

 // save and finish
 $document->save()->finish();

注意:- 使用 Adob​​e Acrobat Reader 打开生成的文档,以便您可以看到 pdf 上的数字签名字段。

答案 1 :(得分:0)

使用https://www.npmjs.com/package/jspdf

    var image = "" // base 64 image

    var url = patientSignature; // url of the image
        
    // Initialize the XMLHttpRequest and wait until file is loaded
    var xhr = new XMLHttpRequest();
    xhr.onload = function () {
        // Create a Uint8Array from ArrayBuffer
        var codes = new Uint8Array(xhr.response);
    
        // Get binary string from UTF-16 code units
        var bin = String.fromCharCode.apply(null, codes);
    
        // Convert binary to Base64
        var b64 ="data:image/jpg;base64,"+ btoa(bin);
        // console.log(b64); //-> "R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs="
    
        var doc = new jsPDF()
        
        doc.addImage(image, "png", 100, 5, 10, 10);
        doc.addImage(bin, "png", 140, 205, 40, 40);
    
        // doc.addImage("data:image/png;base64,"+dataUrl, "JPEG", 100, 5, 10, 10);
        var specialElementHandlers = {
            '#editor': function(element, renderer){
                return true;
            }
        };
        // All units are in the set measurement for the document
        // This can be changed to "pt" (points), "mm" (Default), "cm", "in"
        doc.fromHTML($('#content').get(0), 15, 10, {
            'width': 170, 
            'elementHandlers': specialElementHandlers
        });
        doc.save(store_data.patientName+'.pdf');
    };

    // Send HTTP request and fetch file as ArrayBuffer
    xhr.open('GET', url);
    xhr.responseType = 'arraybuffer';
    xhr.send();     
})

答案 2 :(得分:-2)

您没有指明您是通过 php 生成 PDF 还是创建离线 PDF。

如果您的软件允许签名签名,那么如果离线,这是一个简单的过程。查看软件手册。

如果通过 php 代码创建 PDF,可以这样做。有几种方法可以实现这一点。您需要就要使用数字签名“捕获”的内容做出一些决定。在 Google 上搜索“php 创建数字签名”

相关问题