I am trying to add Digital signature to existing PDF file using FPDI and TCPDF, signature is getting added to the PDF file but in Adobe viewer the signature is not visible, a small rectangular block is visible in the PDF but not text in it (i.e. Signed by John Doe, Date: ............)
Below is my code,
<?php
require_once('./tcpdf/tcpdf.php');
require_once('./tcpdf/tcpdf_import.php');
require_once('./fpdi/fpdi.php');
$pdf = new FPDI('L', 'mm', 'LETTER'); //FPDI extends TCPDF
$pdf->AddPage();
$pages = $pdf->setSourceFile('pan.pdf');
$page = $pdf->ImportPage(1);
$pdf->useTemplate($page, 0, 0);
$certificate = 'file://G:/wamp/www/cert/certificate.crt';
$info = array(
'Name' => 'Nishant',
'Location' => 'Ahmedabad',
'Reason' => 'Testing Signature',
'ContactInfo' => 'http://www.google.com',
);
$pdf->Text(200, 170, 'Test Content');
$pdf->setSignature($certificate, $certificate, 'nishant', '', 2, $info);
$pdf->setSignatureAppearance(200, 170, 40, 20);
$pdf->Output('signed.pdf', 'D');
Attached is the screenshot of the PDF in adobe viewer
Also as per the sample given on Adobe website; https://blogs.adobe.com/security/SampleSignedPDFDocument.pdf
A digital signature looks like below screenshot
If someone can help me out, it will be really appriciated
Please note
I am using self signed certificate, generated using openssl
I have already checked "View signed version" option in adobe viewer
I am looking for a opensource answer of digitally signing PDFs
Thanks
Related
good evening ! I need your help, I am trying to generate several PDFs with the TCPDF, I have been able to create a PDF one by one by passing the ID of the database to the function, but what I need is for it to automatically create several PDFs for each ID that it brings from the database, how can I achieve this?
Controller:
public function reportpdf($id){
ob_start();
$allowance= $this->allowance_m->get_allowance_byid($id);
$this->load->library('tcpdf');
$pdf = new
TCPDF('P','mm','A4',true,'UTF-8',false);
$pdf->AddPage();
$pdf->setXY(12,40);
$txt_pembuka = 'Allowance Report';
//$pdf->SetFontSize(16);
$pdf->SetFont('times', 'B', 16, '', 'false');
$pdf->MultiCell(0, 5, $txt_pembuka, 0, 'C', 0, 2, '', '', true);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->SetFont('times', '', 12, '', 'false');
$pdf->cell(35,5,"Nama");
$pdf->cell(0,5,": ".$allowance->tarjeta);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Periode");
$periode = strtotime($allowance->monto);
$formatperiode = date('F Y',$periode);
$pdf->cell(0,5,": ".$formatperiode);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Uang Makan");
$pdf->cell(0,5,": Rp.".$allowance->nombre);
$pdf->Output();
ob_end_flush();
}
Model:
public function get_allowance_byid($id)
{
$db6 = $this->load->database('db6', TRUE);
$sql = "SELECT * FROM registros WHERE tarjeta='".$id."'";
return $db6->query($sql)->row();
}
If you want to create "several PDFs for each ID" it is recommended to first save the created PDF files in a local folder on your server before making them available to your user instead of prompting the created PDF directly in the browser as in your example. It is not practical to send multiple PDFs at once to the browser.
Add a path and a filename to the Output Method to save the PDF, preferably with an ID so you can find it again:
$pdf->Output('allowance_report_'.$id.'.pdf');
Documentation: https://tcpdf.org/docs/srcdoc/TCPDF/classes-TCPDF/#method_Output
In your case, you can either create multiple PDFs inside the method reportpdf() or make a new method for each file.
If you choose to do it inside your current method you can do something like:
public function reportpdf($id){
$allowance= $this->allowance_m->get_allowance_byid($id);
$this->load->library('tcpdf');
//CREATE YOUR FIRST PDF....
$pdf = new
TCPDF('P','mm','A4',true,'UTF-8',false);
$pdf->AddPage();
... ETC ...
// SAVE THE FIRST PDF BY PROVIDING A PATH AND A FILENAME
$pdf->Output('folder/allowance_report_'.$id.'.pdf');
//CREATE YOUR SECOND PDF....
$pdf = new
TCPDF('P','mm','A4',true,'UTF-8',false);
$pdf->AddPage();
... ETC ...
// SAVE THE SECOND PDF BY PROVIDING A PATH AND A FILENAME
$pdf->Output('folder/another_report_'.$id.'.pdf');
}
If you then want to send the PDFs to the browser I suggest you create a zip file containing all the created PDFs and prompt it through the browser.
I am using TCDPF to generate barcode in invoice. Thus, when I download invoice by clicking "View Invoice" from Orders page, it should download with a barcode generated. At the moment, I am just testing a random number.
In HTMLTemplateInvoice.php, I added the following codes:
// Random number for testing
$barcodeobj = new TCPDFBarcode('1234567890123', 'C39');
// Assign to template
// All other unnecessary variables not displayed
'barcode' => base64_encode($barcodeobj->getBarcodePNG(1, 30, array(255,255,255))),
);
For testing purpose, I added the following codes in invoice.addresses.tab.tpl
<img src="data:image/png;base64,{$barcode}>">
The barcode is displayed. Unfortunately, it is dislaying as a page and it is not generated in the invoice. The invoice will not download too.
I am using Prestashop 1.7.4.2 and upgraded to PHP 7.2.
I've tried this code
$barcodeobj = new TCPDFBarcode("123456789123", 'C39');
$Barcode = $barcodeobj->getBarcodePngData(2, 40, array(0,0,0));
and then i created a new pdf :
$Barcode_pdf = new TCPDF('P', 'mm', 'A5', true, 'UTF-8', false);
$Barcode_pdf->SetMargins(7, 10.4, 8);
$Barcode_pdf->setImageScale(0.5);
$Barcode_pdf->AddPage();
$Barcode_pdf->Image('#'.$Barcode,'10','20','60' );
$Barcode_pdf->Output('Barcode.pdf', 'I')
you can attach it to email if you want but change the output type to 'S':
$Barcode_attachment = array();
$Barcode_attachment['content'] = $Barcode_pdf->Output('Barcode.pdf', 'S');
$Barcode_attachment['name'] = 'Barcode.pdf'; // getting pdf file name
$Barcode_attachment['invoice']['mime'] = 'application/pdf';
$Barcode_attachment['mime'] = 'application/pdf';
and then just add $Barcode_attachment to your email .
In my Certificate plugin in Moodle, I can choose from 4 signatures. The signature images are shown in the certificate and loaded trough CERT_IMAGE_SIGNATURE:
certificate_print_image($pdf, $certificate, CERT_IMAGE_SIGNATURE, $sigx, $sigy, '', '');
The file names are jacob.png vanessa.png stan.png `lilly.png.
How to add automatically text under the image? For example the names of the person from the signature image?
If I choose the file jacob.png from the dropdown list, It should load under the image also the names, here "Jacob Svenson".
I would do something like this
in /mod/certificate/type/xxx/certificate.php
if (!empty($certificate->printsignature)) {
certificate_print_image($pdf, $certificate, CERT_IMAGE_SIGNATURE, $sigx, $sigy, '', '');
$signame = get_string($certificate->printsignature, 'certificate');
certificate_print_text($pdf, $x, $y + 20, 'C', $fontserif, '', 20, $signame);
}
and in /mod/certificate/lang/en/certificate.php use the file name as the string id.
$string['jacob.png'] = 'Jacob Svenson';
I am using TCPDF library to write a custom size label with background image and multiple text blocks on it.
The user when sees the preview on screen of the PDF it should show in horizontal, but for printing, I need the full page rotated -90 degrees.
How can I just rotate the whole page for printing version without having to move anything?
Basically:
In my case I've already had to use a new document format for the special sizes my document required.
So I've duplicated that format, created one for Landscape and one for Portrait.
Then based on the $preview variable, if previewing I'm rendering the normal landscape document, but if not previewing, I'm using the Portrait format and orientation and also starting the transformation and rotating everything on page.
Hope this helps someone I've found no other "quick" way to accomplish this kind of full-page rotation.
<?php
// #1 get the preview attribute from
// the form that was submitted from the user
$preview= isset($_POST['preview'])?(int)$_POST['preview']:0;
// load TCPDF for CodeIgniter as a library
$this->load->library('Pdf');
// #2 set default orientation and format
$orientation='L';
$format='MAKE-L';
// #3 if not previewing, switch orientation and format to portrait
if (!$preview) {
$orientation='P';
$format='MAKE-P';
}
// create new pdf object
// (same as doing new TCPDF(), it is just the CodeIgniter wrapper)
$pdf = new Pdf($orientation, 'mm', $format, true, 'UTF-8', false);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(0, 0, 0);
$pdf->AddPage($orientation, $format);
// #4 if not previewing, start transformation
// and rotate everything before inserting any content
if (!$preview) {
// Start Transformation
$pdf->StartTransform();
// Rotate counter-clockwise centered by (x,y)
$pdf->Rotate(-90, 70, 70); // <-- TODO: test this very well because 70 and 70 was just guessing, there is no math behind that so not sure if it will work always
}
// put your content here,
// for example set font and add a text
$pdf->SetFont('times', '', 7, '', true);
$pdf->writeHTMLCell(0, 0, 25.4, 2, 'lot number', 0, 1, 0, true, '', true);
/// end content
// #5 if not in preview mode, finish the transformation
if (!$preview) {
// Stop Transformation
$pdf->StopTransform();
}
$pdf->Output('example.pdf', 'I');
/**
* Last but very important note:
* I have added my formats in tcpdf/includes/tcpdf_static.php file.
* >> MAKE-L for Landscape
* >> MAKE-P for Portrait
*/
public static $page_formats = array(
// Make
'MAKE-L' => array( 396.850, 425.196), // = ( h 140 x w 150 ) mm
// Make
'MAKE-P' => array( 425.196, 396.850 ), // = ( h 140 x w 150 ) mm
// .. rest of formats here ../
);
The setPageFormat() method should do the job. You also can pass the parameter to the $format parameter of AddPage():
$pdf->AddPage($orientation, ['format' => $format, 'Rotate' => -90]);
I'm trying to hide the header page number on the first page using this example I found here. Which only works if I use it with footer-html and doesn't show/hide anything if I use it with header-html. Originally I was trying to augment this solution which also worked using footer-html, but since I couldn't get it to work in the header I kept on searching. I've tried it with and without 'header-center' => '[[page]]' in case using this with header-html caused conflicts. Anyone been able to get this to work in the headers recently? I'm using PHPWKHTMLtoPDF wrapper version 1.2.6-dev if that helps with a up to date version of WKHTMLtoPDF, since the newest version of PHPWKHTMLtoPDF uses namespaces and we're using CodeIgniter 2.x-dev, which doesn't support them (or play well can't remember).
// Create document PDF
$pdf = new $this->wkhtmltopdf;
// Locate WkHtmlToPdf executable for Windows
if( $pdf->getIsWindows() )
{
$pdf->setOptions( array( 'binPath' => 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe',
'no-outline',
'encoding' => 'UTF-8',
'margin-top' => 30,
'margin-right' => 20,
'margin-bottom' => 30,
'margin-left' => 20,
// Default page options
'disable-smart-shrinking',
'user-style-sheet' => 'pdf.css',
'header-html' => dirname(__FILE__) . '\..\views\wkhtmltopdf\header.html'
) );
}
// Generate document fields
$docInputs = $this->generate_inputs( $inputs, json_decode( $this->load->file( APPPATH . '/mapping/' . $document['mapping'], TRUE ), TRUE ) );
// Merge document fields into HTML exported Word files
$docHTML = $this->parser->parse( 'docs/' . $document['html'], $docInputs, TRUE );
// Add HTML as page, along with option for page header
$pdf->addPage( $docHTML, array( 'header-center' => '[[page]]',
'header-spacing' => '10',
'header-font-name' => 'Times New Roman'
) );
You need to add the <!DOCTYPE html> to the header file, WKHTMLtoPDF issue #46 for version 0.12
I'm posting this answer because this happened to me and this might also be a reason.
I'v also noticed if you set the header css to this. It will not show the header.
html{
width: 100%;
height: 100%;
}
Make sure you have version with patched qt