PHP code example of jsignpdf / jsignpdf-php

1. Go to this page and download the library: Download jsignpdf/jsignpdf-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jsignpdf / jsignpdf-php example snippets


use Jeidison\JSignPDF\JSignPDF;
use Jeidison\JSignPDF\Sign\JSignParam;

$param = JSignParam::instance();
$param->setCertificate(file_get_contents('/path/to/file/certificate.pfx'));
$param->setPdf(file_get_contents('/path/to/file/pdf_to_sign.pdf'));
$param->setPassword('certificate_password');

$jSignPdf   = new JSignPDF($param);
$fileSigned = $jSignPdf->sign();
file_put_contents('/path/to/file/file_signed.pdf', $fileSigned);

$param->setIsUseJavaInstalled(true);

$param->setJavaPath('/path/to/bin/java');

$param->setJavaOptions(['-Duser.home=/tmp/jsignpdf-home']);
$param->setEnvironmentVariables(['JSIGNPDF_HOME' => '/tmp/jsignpdf-home']);

$param->setJSignPdfPath('/path/to/jsignpdf');

$params->getJSignPdfDownloadUrl('the url to download the zip here');
$params->setJavaDownloadUrl('the url to download the .tar.gz here');

$param->setIsOutputTypeBase64(true);

$param->setTempPath('/path/temp/to/sign/files/');

$param->setJSignParameters(['-kst' => 'PKCS12', '-ts' => 'https://freetsa.org/tsr']);

$param->setJSignParameters(['-a', '--overwrite', '-kst' => 'PKCS12']);

$param->setJSignParameters(['--l2-text' => '-tsp']);   // -tsp is the text, not an option

$param->addJSignParameters(['-ha' => 'SHA512']);

$param = JSignParam::instance();
$param->setPdf(file_get_contents('/path/to/file/pdf_to_sign.pdf'));

$jSignPdf = new JSignPDF($param);
$fields = $jSignPdf->getSignatureFields();

foreach ($fields as $field) {
    echo $field->getName();
    echo $field->getPage();
    echo $field->isSigned() ? 'signed' : 'blank';
}

$param->setCertificate(file_get_contents('/path/to/file/certificate.pfx'));
$param->setPassword('certificate_password');
$param->setSignatureField('Customer Signature');

$fileSigned = JSignPDF::instance($param)->sign();

$param->setKeyPassword('private key password');      // -kp
$param->setOwnerPassword('owner password');          // -opwd
$param->setUserPassword('user password');            // -upwd
$param->setTsaCertPassword('tsa cert password');     // -tscp
$param->setTsaPassword('tsa password');              // -tsp

$param->setJSignParameters([
    '-ts'  => 'https://freetsa.org/tsr',
    '-ta'  => 'PASSWORD',
    '-tsu' => 'jhon',
    '-tsp' => 'tsa password',
]);

$param->setJSignParameters(['-kst' => 'PKCS12', '--overwrite']);
bash
$ composer 
bash
docker compose run --rm php php example/index.php