{"id":2080,"date":"2022-07-06T12:59:50","date_gmt":"2022-07-06T07:59:50","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=2080"},"modified":"2022-07-06T12:59:52","modified_gmt":"2022-07-06T07:59:52","slug":"php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/","title":{"rendered":"PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">index.php<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\nrequire('fpdf\/fpdf.php');\n\nclass PDF extends FPDF {\n\n  \/\/ Function to set the document header\n  function Header() {\n\n    global $title;\n    \n    \/\/ Sets font to Arial bold 15\n    $this-&gt;SetFont('Arial', 'B', 15);\n    \n    \/\/ Calculate string length\n    $w = $this-&gt;GetStringWidth($title) + 6;\n    $this-&gt;SetX((210 - $w) \/ 2);\n    \n    \/\/ Set drawing color\n    $this-&gt;SetDrawColor(0, 80, 180);\n    \n    \/\/ It defines the grey color for filling\n    $this-&gt;SetFillColor(105, 105, 105);\n    \n    \/\/ Sets the text color\n    $this-&gt;SetTextColor(255, 0, 0);\n    \n    \/\/ Set the line width to 1 mm)\n    $this-&gt;SetLineWidth(1);\n    \n    \/\/ Prints a cell Title\n    $this-&gt;Cell($w, 9, $title, 1, 1, 'C', 1);\n    \n    \/\/ Line break\n    $this-&gt;Ln(10);\n  }\n  \n  \/\/ Function to set the document footer\n  function Footer() {\n\n    \/\/ Set Y Position from bottom\n    $this-&gt;SetY(-20);\n    \n    \/\/ Sets font to Arial italic 10\n    $this-&gt;SetFont('Arial', 'I', 10);\n    \n    \/\/ Sets the Text color in gray\n    $this-&gt;SetTextColor(128);\n    \n    \/\/ Prints a cell with Page number\n    $this-&gt;Cell(0, 10, 'Page '\n      . $this-&gt;PageNo(), 0, 0, 'C');\n  }\n  \n  \/\/ Function to set the title for a tutorial\n  function tutorialTitle($num, $label) {\n\n    \/\/ Sets font to Arial 12\n    $this-&gt;SetFont('Arial', '', 12);\n    \n    \/\/ Sets to fill Background color with Light grey\n    $this-&gt;SetFillColor(211, 211, 211);\n    \n    \/\/ Prints a cell with Title for tutorial\n    $this-&gt;Cell(0, 6, &quot;Chapter $num : $label&quot;, 0, 1, 'L', 1);\n    \n    \/\/ Line break\n    $this-&gt;Ln(4);\n  }\n  \n  \/\/ Function to set the content from a external file\n  function tutorialContent($file) {\n\n    \/\/ Read text file\n    $f = fopen($file, 'r');\n    $txt = fread($f, filesize($file));\n    fclose($f);\n    \n    \/\/ Sets the font to Times 12\n    $this-&gt;SetFont('Times', '', 12);\n    \n    \/\/ It prints texts with line breaks\n    $this-&gt;MultiCell(0, 5, $txt);\n    \n    \/\/Puts a Line break\n    $this-&gt;Ln();\n    \n    \/\/ Set font in italics\n    $this-&gt;SetFont('', 'I');\n    \n    \/\/ Prints a cell\n    $this-&gt;Cell(0, 5, '(end of content)');\n  }\n  \n  function showTutorial($num, $title, $file) {\n\n    \/\/ Add a new page\n    $this-&gt;AddPage();\n    $this-&gt;tutorialTitle($num, $title);\n    $this-&gt;tutorialContent($file);\n  }\n}\n\n\/\/ Initiate a PDF object\n$pdf = new PDF();\n$title = 'C Programming Language';\n\n\/\/ Sets the document title\n$pdf-&gt;SetTitle($title);\n\n\/\/ Sets the document author name\n$pdf-&gt;SetAuthor('gfg author name');\n\n$pdf-&gt;showTutorial(\n  1,\n  'C Language Introduction',\n  'cIntro.txt'\n);\n\n$pdf-&gt;showTutorial(\n  2,\n  'C Programming Language Standard',\n  'cStandard.txt'\n);\n\n$pdf-&gt;showTutorial(\n  3,\n  'Importance of function prototype in C',\n  'cPrototype.txt'\n);\n\n$pdf-&gt;Output();\n?&gt;<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>index.php<\/p>\n","protected":false},"author":1,"featured_media":1762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-2080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript<\/title>\n<meta name=\"description\" content=\"index.php &lt;?php require(&#039;fpdf\/fpdf.php&#039;); class PDF extends FPDF { \/\/ Function to set the document header function Header() { global $title; \/\/ Sets\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript\" \/>\n<meta property=\"og:description\" content=\"index.php &lt;?php require(&#039;fpdf\/fpdf.php&#039;); class PDF extends FPDF { \/\/ Function to set the document header function Header() { global $title; \/\/ Sets\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-06T07:59:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-06T07:59:52+00:00\" \/>\n<meta name=\"author\" content=\"Furqan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Furqan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript","description":"index.php &lt;?php require('fpdf\/fpdf.php'); class PDF extends FPDF { \/\/ Function to set the document header function Header() { global $title; \/\/ Sets","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:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/","og_locale":"en_US","og_type":"article","og_title":"PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript","og_description":"index.php &lt;?php require('fpdf\/fpdf.php'); class PDF extends FPDF { \/\/ Function to set the document header function Header() { global $title; \/\/ Sets","og_url":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-07-06T07:59:50+00:00","article_modified_time":"2022-07-06T07:59:52+00:00","author":"Furqan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Furqan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript","datePublished":"2022-07-06T07:59:50+00:00","dateModified":"2022-07-06T07:59:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/"},"wordCount":13,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/","url":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/","name":"PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","datePublished":"2022-07-06T07:59:50+00:00","dateModified":"2022-07-06T07:59:52+00:00","description":"index.php &lt;?php require('fpdf\/fpdf.php'); class PDF extends FPDF { \/\/ Function to set the document header function Header() { global $title; \/\/ Sets","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","width":880,"height":495,"caption":"Default Featured Image"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/php-7-fpdf-example-create-pdf-document-from-text-file-using-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript"}]},{"@type":"WebSite","@id":"https:\/\/www.edopedia.com\/blog\/#website","url":"https:\/\/www.edopedia.com\/blog\/","name":"Edopedia","description":"Coding\/Programming Blog","publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edopedia.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.edopedia.com\/blog\/#organization","name":"Edopedia","url":"https:\/\/www.edopedia.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","width":400,"height":100,"caption":"Edopedia"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339","name":"Furqan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","caption":"Furqan"},"description":"Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.","sameAs":["http:\/\/www.edopedia.com\/blog\/","trulyfurqan"],"url":"https:\/\/www.edopedia.com\/blog\/author\/furqan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/comments?post=2080"}],"version-history":[{"count":0,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2080\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/1762"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=2080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=2080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=2080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}