{"id":4856,"date":"2022-08-24T20:31:09","date_gmt":"2022-08-24T15:01:09","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/"},"modified":"2022-08-24T20:31:09","modified_gmt":"2022-08-24T15:01:09","slug":"solved-form-to-php-to-pdf-conversion-assistance","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/","title":{"rendered":"[Solved] Form to PHP to PDF: Conversion Assistance"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-46564726\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"46564726\" data-parentid=\"46564284\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I&#8217;ve recently use pdftk (server) to do so: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.pdflabs.com\/tools\/pdftk-server\/\">https:\/\/www.pdflabs.com\/tools\/pdftk-server\/<\/a><\/p>\n<p>First, install it on a webserver or locally.<\/p>\n<p>Then, here&#8217;s a PHP class I adapted from the web (copy it and name it <strong>PdfFormToPdftk.php<\/strong>):<\/p>\n<pre><code>&lt;?php\n\nclass PdfFormToPdftk {\n    \/*\n     * Path to raw PDF form\n     * @var string\n     *\/\n    private $pdfurl;\n\n     \/*\n     * Path to PDFKTK\n     * @var string\n     *\/\n    private $pdftkpath;\n\n    \/*\n     * Path to temp files\n     * @var string\n     *\/\n    private $tmppath;\n\n    \/*\n     * Errors\n     * @var string\n     *\/\n    private $errors;\n\n    \/*\n     * Last command done\n     * @var string\n     *\/\n    private $lastcmd;\n\n    \/*\n     * Form data\n     * @var array\n     *\/\n    private $data;\n\n    \/*\n     * Path to filled PDF form\n     * @var string\n     *\/\n    private $output;\n\n    \/*\n     * Flag for flattening the file\n     * @var string\n     *\/\n    private $flatten;\n\n    public function __construct($pdfurl, $data, $tmppath, $pdftkpath=\"\/usr\/bin\/pdftk\") {\n        $this-&gt;pdfurl = $pdfurl;\n        $this-&gt;data = $data;\n        $this-&gt;tmppath = $tmppath;\n        $this-&gt;pdftkpath = $pdftkpath;\n    }\n\n    private function tempfile() {\n        return tempnam($this-&gt;tmppath, gethostname());\n    }\n\n    public function fields($pretty = false) {\n        $tmp = $this-&gt;tempfile();\n\n        exec(\"{$this-&gt;pdftkpath} {$this-&gt;pdfurl} dump_data_fields &gt; {$tmp}\");\n        $con = file_get_contents($tmp);\n\n        unlink($tmp);\n        return $pretty == true ? nl2br($con) : $con;\n    }\n\n    private function makeFdf() {\n        $fdf=\"%FDF-1.2\n        1 0 obj&lt;&lt;\/FDF&lt;&lt; \/Fields[\";\n\n        foreach ($this-&gt;data as $key =&gt; $value) {\n            $fdf .= '&lt;&lt;\/T(' . $key . ')\/V(' . $value . ')&gt;&gt;';\n        }\n\n        $fdf .= \"] &gt;&gt; &gt;&gt;\n        endobj\n        trailer\n        &lt;&lt;\/Root 1 0 R&gt;&gt;\n        %%EOF\";\n\n        $fdf_file = $this-&gt;tempfile();\n        file_put_contents($fdf_file, $fdf);\n\n        return $fdf_file;\n    }\n\n    public function flatten() {\n        $this-&gt;flatten = ' flatten';\n        return $this;\n    }\n\n    private function generate() {\n\n        $fdf = $this-&gt;makeFdf();\n        $this-&gt;output = $this-&gt;tempfile();\n        $cmd = \"{$this-&gt;pdftkpath} {$this-&gt;pdfurl} fill_form {$fdf} output {$this-&gt;output}{$this-&gt;flatten} 2&gt;&amp;1\";\n        $this-&gt;lastcmd = $cmd;\n        exec($cmd, $outputAndErrors, $returnValue);\n        $this-&gt;errors = $outputAndErrors;\n        unlink($fdf);\n    }\n\n    public function save($path = null) {\n        if (is_null($path)) {\n            return $this;\n        }\n\n        if (!$this-&gt;output) {\n            $this-&gt;generate();\n        }\n\n        $dest = pathinfo($path, PATHINFO_DIRNAME);\n        if (!file_exists($dest)) {\n            mkdir($dest, 0775, true);\n        }\n\n        if (!copy($this-&gt;output, $path)) {\n            echo \"failed to copy $path...\\n\";\n        }\n        unlink($this-&gt;output);\n\n        $this-&gt;output = $path;\n\n        return $this;\n    }\n\n    public function download() {\n        if (!$this-&gt;output) {\n            $this-&gt;generate();\n        }\n\n        $filepath = $this-&gt;output;\n        if (file_exists($filepath)) {\n\n            header('Content-Description: File Transfer');\n            header('Content-Type: application\/pdf');\n            header('Content-Disposition: attachment; filename=\" . uniqid(gethostname()) . \".pdf');\n            header('Expires: 0');\n            header('Cache-Control: must-revalidate');\n            header('Pragma: public');\n            header('Content-Length: ' . filesize($filepath));\n\n            readfile($filepath);\n\n            exit;\n        }\n    }\n\n}\n<\/code><\/pre>\n<p>You could use it like this in some script:<\/p>\n<pre><code>&lt;?php\nrequire_once 'PdfFormToPdftk.php';\n\n$datas = ['firstname' =&gt; 'Foo', 'lastname' =&gt; 'Bar'];\n$output_file=\"yourfile.pdf\";\n$template_pdf_file=\"templatefile.pdf\"; \/\/where your virgin pdf template contains at least 'firstname' and 'lastname' as editable fields. You might want to use Adobe Acrobat Pro and save it as a Adobe Static PDF Form\n$path_to_pdftk_server=\"\/opt\/pdflabs\/pdftk\/bin\/pdftk\"; \/\/ type 'which pdftk' in your console to find yours\n\n$pdf = new PdfFormToPdftk($template_pdf_file, $datas, \"https:\/\/stackoverflow.com\/\", $path_to_pdftk_server);\n$file = $pdf-&gt;save($output_file);\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">9<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Form to PHP to PDF: Conversion Assistance <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I&#8217;ve recently use pdftk (server) to do so: https:\/\/www.pdflabs.com\/tools\/pdftk-server\/ First, install it on a webserver or locally. Then, here&#8217;s a PHP class I adapted from the web (copy it and name it PdfFormToPdftk.php): &lt;?php class PdfFormToPdftk { \/* * Path to raw PDF form * @var string *\/ private $pdfurl; \/* * Path to &#8230; <a title=\"[Solved] Form to PHP to PDF: Conversion Assistance\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/\" aria-label=\"More on [Solved] Form to PHP to PDF: Conversion Assistance\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[489,339],"class_list":["post-4856","post","type-post","status-publish","format-standard","hentry","category-solved","tag-pdf","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Form to PHP to PDF: Conversion Assistance - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Form to PHP to PDF: Conversion Assistance - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I&#8217;ve recently use pdftk (server) to do so: https:\/\/www.pdflabs.com\/tools\/pdftk-server\/ First, install it on a webserver or locally. Then, here&#8217;s a PHP class I adapted from the web (copy it and name it PdfFormToPdftk.php): &lt;?php class PdfFormToPdftk { \/* * Path to raw PDF form * @var string *\/ private $pdfurl; \/* * Path to ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-24T15:01:09+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Form to PHP to PDF: Conversion Assistance\",\"datePublished\":\"2022-08-24T15:01:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/\"},\"wordCount\":68,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"pdf\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/\",\"name\":\"[Solved] Form to PHP to PDF: Conversion Assistance - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-24T15:01:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Form to PHP to PDF: Conversion Assistance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Form to PHP to PDF: Conversion Assistance - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Form to PHP to PDF: Conversion Assistance - JassWeb","og_description":"[ad_1] I&#8217;ve recently use pdftk (server) to do so: https:\/\/www.pdflabs.com\/tools\/pdftk-server\/ First, install it on a webserver or locally. Then, here&#8217;s a PHP class I adapted from the web (copy it and name it PdfFormToPdftk.php): &lt;?php class PdfFormToPdftk { \/* * Path to raw PDF form * @var string *\/ private $pdfurl; \/* * Path to ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/","og_site_name":"JassWeb","article_published_time":"2022-08-24T15:01:09+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Form to PHP to PDF: Conversion Assistance","datePublished":"2022-08-24T15:01:09+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/"},"wordCount":68,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["pdf","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/","url":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/","name":"[Solved] Form to PHP to PDF: Conversion Assistance - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-24T15:01:09+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-form-to-php-to-pdf-conversion-assistance\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Form to PHP to PDF: Conversion Assistance"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=4856"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4856\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=4856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=4856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=4856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}