是否可以使用PHP在PDF文档中添加签名字段?我没有“数字签名”文档。我只添加一个签名字段。

2022-08-30 16:13:12

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

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

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

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

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

谢谢。


答案 1

正如您已经告诉过的那样,您已经在这里浏览了seta符号库文档

使用 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();

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


答案 2

使用 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();     
})

推荐