-
-
Save jpdroid/8e1413c6fe0372f302a41b822b309dd1 to your computer and use it in GitHub Desktop.
assina um pdf com cert digital a1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once('vendor/autoload.php'); | |
| use setasign\Fpdi\Tcpdf\Fpdi; | |
| Class PDF extends FPDI{ | |
| private $nome; | |
| private $cpf; | |
| function __construct($nome,$cpf){ | |
| parent::__construct(); | |
| $this->nome = $nome; | |
| $this->cpf = $cpf; | |
| } | |
| function Header(){ | |
| } | |
| function Footer(){ | |
| // Positionnement à 1,5 cm du bas | |
| $this->SetY(-10); | |
| // Police Arial italique 8 | |
| $this->SetFont('Helvetica','',6); | |
| // Numéro de page | |
| $textoFooter = "DOCUMENTO ASSINADO DIGITALMENTE POR $this->nome CPF $this->cpf VERIFIQUE O DOCUMENTO EM https://verificador.iti.gov.br"; | |
| $this->Cell(0,10,$textoFooter,'T',0,'C'); | |
| } | |
| } | |
| // echo ; | |
| $diretorio = getcwd() . '/'; | |
| $nomeCertPFX = $diretorio . 'a.pfx'; | |
| $documentoParaAssinar = $diretorio . 'contrato.pdf'; | |
| $nomeCertCRT = $diretorio . 'tcpdf.crt'; | |
| $password = '15151515'; | |
| // * Gera o .crt a partir do .pfx | |
| if (!file_exists('tcpdf.crt')){ | |
| shell_exec("openssl pkcs12 -in $nomeCertPFX -out $nomeCertCRT -nodes -passin pass:$password"); | |
| } | |
| $p = file_get_contents($nomeCertCRT); | |
| //Endereço do arquivo do certificado | |
| //Obs.: Tentei usar o certificado no formato PFX e não funcionou | |
| //Para converter use o comando no Prompt do Windows ou Terminal do Linux: | |
| //openssl pkcs12 -in certificado.pfx -out tcpdf.crt -nodes | |
| // $cert = '/home/tuchinski/Documentos/apoema/pdf/tcpdf.crt'; | |
| $pkcs12 = file_get_contents($nomeCertPFX); | |
| // aqui a gente pega o certificado .crt, mas esse cara a gente tem que gerar | |
| $cert = openssl_x509_read( $p ); | |
| $cert_parsed = openssl_x509_parse( $cert ,true); | |
| // print_r($cert_parsed); | |
| $nome_cpf = explode(":",$cert_parsed['subject']['CN']); | |
| $res = []; | |
| $openSSL = openssl_pkcs12_read($pkcs12, $res, $password); | |
| if(!$openSSL) { | |
| throw new ClientException("Error: ".openssl_error_string()); | |
| } | |
| // // this is the CER FILE | |
| // file_put_contents('CERT.cer', $res['pkey'].$res['cert'].implode('', $res['extracerts'])); | |
| // // this is the PEM FILE | |
| // $cert = $res['cert'].implode('', $res['extracerts']); | |
| // file_put_contents('KEY.pem', $cert); | |
| // aqui a gente pega o certificado .pfx | |
| if (openssl_pkcs12_read($pkcs12, $cert_info, $password)) { | |
| // echo "Certificate read\n"; | |
| } else { | |
| echo "Error: Unable to read the cert store.\n"; | |
| exit; | |
| } | |
| //Informações da assinatura - Preencha com os seus dados | |
| $info = array( | |
| 'Name' => '', | |
| 'Location' => '', | |
| 'Reason' => '', | |
| 'ContactInfo' => '', | |
| ); | |
| $pdf = new PDF($nome_cpf[0],$nome_cpf[1]); | |
| //Configura a assinatura. Para saber mais sobre os parâmetros | |
| //consulte a documentação do TCPDF, exemplo 52. | |
| //Não esqueça de mudar 'senha' para a senha do seu certificado | |
| // var_dump($cert); | |
| //Importa uma página | |
| $numPages = $pdf->setSourceFile($documentoParaAssinar); | |
| // print_r($pdf->numPages()); | |
| // print_r($numPages); | |
| for ($i=0; $i < $numPages; $i++) { | |
| # code... | |
| $pdf->AddPage(); | |
| // $text = "Documento assinado digitalmente por <b>$nome_cpf[0]</b>, CPF $nome_cpf[1]"; | |
| // $pdf->writeHTML($text, true, 0, true, 0); | |
| $tplId = $pdf->importPage($i+1); | |
| // $pdf->setSignature('file://'.$cert, 'file://'.realpath($cert), '','', 2, $info); | |
| $pdf->setSignature($cert_info['cert'], $cert_info['pkey'], '','', 2, $info); | |
| $pdf->useTemplate($tplId, 0, 0); //Importa nas medidas originais | |
| // print a line of text | |
| $pdf->setSignatureAppearance(10,10,10,10,1); | |
| } | |
| //Manda o PDF pra download | |
| $pdf->Output(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment