{"id":2109,"date":"2021-09-01T13:13:14","date_gmt":"2021-09-01T12:13:14","guid":{"rendered":"https:\/\/fhumanes.es\/blog\/?page_id=2109"},"modified":"2021-09-01T13:13:14","modified_gmt":"2021-09-01T12:13:14","slug":"guia-25-generar-codigo-pdf417","status":"publish","type":"page","link":"https:\/\/fhumanes.es\/blog\/guias-desarrollo\/guia-25-generar-codigo-pdf417\/","title":{"rendered":"Gu\u00eda 25 &#8211; Generar c\u00f3digo PDF417"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-1284\" src=\"https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/07\/guia1_008.png\" alt=\"\" width=\"150\" height=\"150\" srcset=\"https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/07\/guia1_008.png 200w, https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/07\/guia1_008-150x150.png 150w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/>David Venegas me explica que en las facturas en Chile se utiliza un c\u00f3digo de barras de 2D <strong>PDF417<\/strong> para imprimir en las facturas. El formato, el conjunto de datos y su disposici\u00f3n, est\u00e1 fijado en la normativa que tiene Chile.<\/p>\n<p>Le es necesario crear este\u00a0 c\u00f3digo e incluirlo en la factura PDF que est\u00e1 haciendo siguiendo <a href=\"https:\/\/fhumanes.es\/blog\/impresos\/crear-factura-en-word-excel-o-pdf-y-enviar-email\/\" target=\"_blank\" rel=\"noopener\">este ejemplo<\/a>. As\u00ed pues, una vez que he hecho el ejemplo lo comparto por si fuese de inter\u00e9s para otros usuarios.<\/p>\n<h4>Objetivo<\/h4>\n<p>Crear imagen PNG de un c\u00f3digo <strong>PDF417<\/strong> para despu\u00e9s utilizar dicha imagen en cualquier otro documento.<\/p>\n<h4>Soluci\u00f3n t\u00e9cnica<\/h4>\n<p>Como en muchos otros casos he ido a GITHUB y he buscado una librer\u00eda que satisfaga las caracter\u00edsticas de c\u00f3digo PDF417 que ten\u00eda. La librer\u00eda escogida es:\u00a0 <a href=\"https:\/\/github.com\/ihabunek\/pdf417-php\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/ihabunek\/pdf417-php<\/a><\/p>\n<p>Pod\u00e9is probar el ejemplo en: <a href=\"https:\/\/fhumanes.com\/pdf417\/\" target=\"_blank\" rel=\"noopener\">https:\/\/fhumanes.com\/pdf417\/<\/a><\/p>\n<p>La verdad es que todo es muy sencillo porque la librer\u00eda est\u00e1 muy bien escrita, es f\u00e1cil de integrar y es muy f\u00e1cil personalizarla con las caracter\u00edsticas que se requiera.<\/p>\n<p>La imagen la creo con este c\u00f3digo \u00ab<strong>generate_pdf417.php<\/strong>\u00bb<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\n\/\/ https:\/\/github.com\/ihabunek\/pdf417-php,           source of Code\r\n\r\n\/* \r\nOccurs after record was updated  or added\r\n\r\n$values     - array of values has been written to the database.\r\n              To access specific field value use $values[\"FieldName\"]\r\n              \"dict\" is an alternate name for this parameter.              \r\n$where      - WHERE clause that points to the edited record. Example: ID=19              \r\n$oldvalues  - array with replaced field values. To access specific column value use\r\n              $oldvalues[\"FieldName\"]              \r\n$keys       - array of key column values that point to the edited record. To access\r\n              specific key column use $keys[\"KeyFieldName\"]              \r\n$inline     - equals to true when the Inline Edit in process, false otherwise              \r\n$pageObject - an object of Page class representing the current page  \r\n*\/\r\n\r\n\/\/ Load the PDF417 library classes\r\nrequire_once __DIR__ . '\/pdf417_0.3\/autoload.php'; \r\n\r\nuse BigFish\\PDF417\\PDF417;\r\nuse BigFish\\PDF417\\Renderers\\ImageRenderer;\r\n\/\/ use BigFish\\PDF417\\Renderers\\SvgRenderer;\r\n\r\n\/\/ Create a basic PDF417 code\r\n$pdf417 = new PDF417();\r\n$pdf417-&gt;setColumns(20); \/\/ Muy importante para campos de gran tama\u00f1o. M\u00e1ximo 30\r\n$pdf417-&gt;setSecurityLevel(5); \/\/ Muy importante fijar nivel de seguridad de c\u00f3digo. M\u00e1ximo 8\r\n\r\n$data = $pdf417-&gt;encode($values['Text']); \r\n\r\n\/\/ Set advanced options\r\n$renderer = new ImageRenderer([\r\n  'format' =&gt; 'png',\r\n  'color' =&gt; '#000000',\r\n  'bgColor' =&gt; '#FFFFFF',\r\n  'scale' =&gt; 5,\r\n]);\r\n\r\n$image = $renderer-&gt;render($data);\r\n\r\n\/\/ Save it to a file\r\n$fileName = 'PDF417_'.$values['id'].'.png';\r\n$file = substr(__DIR__, 0, -6);  \/\/ root of file\r\n$file = $file.'files\/'.$fileName;\r\n$image-&gt;save($file);\r\n\r\n\/\/ Save the new file in Database\r\n$size = filesize($file);\r\n$fileArray = [];\r\n$fileArray[0][name]= 'files\\\/'.$fileName;\r\n$fileArray[0][usrName]= $fileName;\r\n$fileArray[0][size]= $size;\r\n$fileArray[0][type]= 'image\\\/png';\r\n$fileArray[0][searchStr] = $fileName.',!:sStrEnd'; \r\n$fileFileSystem=my_json_encode($fileArray);\r\n            \r\n\/\/ Update in DB the file\r\n$id_key = $values['id']; \r\n$sql=\"Update prueba_pdf417 set File = '$fileFileSystem' where id = $id_key\";\r\n$res=db_exec($sql,$conn);\r\n?&gt;<\/pre>\n<p>Os dejo el ejemplo para que lo pod\u00e1is instalar en vuestros Windows.<\/p>\n<p>Para cualquier consulta sobre este ejemplo o lo que necesit\u00e9is, contactar conmigo a trav\u00e9s de mi email. <a href=\"mailto:fernandohumanes@gmail.com\">fernandohumanes@gmail.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>David Venegas me explica que en las facturas en Chile se utiliza un c\u00f3digo de barras de 2D PDF417 para imprimir en las facturas. El formato, el conjunto de datos y su disposici\u00f3n, est\u00e1 fijado en la normativa que tiene Chile. Le es necesario crear este\u00a0 c\u00f3digo e incluirlo en la factura PDF que est\u00e1 &hellip; <a href=\"https:\/\/fhumanes.es\/blog\/guias-desarrollo\/guia-25-generar-codigo-pdf417\/\" class=\"more-link\">Seguir leyendo <span class=\"screen-reader-text\">Gu\u00eda 25 &#8211; Generar c\u00f3digo PDF417<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1254,"menu_order":54,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2109","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages\/2109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/comments?post=2109"}],"version-history":[{"count":3,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages\/2109\/revisions"}],"predecessor-version":[{"id":2113,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages\/2109\/revisions\/2113"}],"up":[{"embeddable":true,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages\/1254"}],"wp:attachment":[{"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/media?parent=2109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}