I have a HTML/PHP form in which i am inserting vales in database.its working fine.
What i am trying once user fill out the form,there should be a option of save as pdf
& Print
How to convert html form in to pdf.
Need suggestions.

Dani AI

Generated

Good question. What you actually need depends on whether you want to draw a PDF with API calls (as kevindougans showed with PDFlib) or render your existing HTML into a PDF. PDFlib is great for low-level control but it will not automatically convert an HTML form to PDF, and it is a commercial library that requires licensing in production. For most form-confirmation or invoice-style PDFs, an HTML-to-PDF renderer like dompdf or mPDF is simpler because you can reuse your form’s HTML/CSS. (pdflib.com, dompdf.github.io, mpdf.github.io)

To answer jacob21’s follow-up: put the PDF code in a separate handler (for example, pdf.php). After saving the form to the database, redirect the user to pdf.php with an ID, or post to it. In pdf.php, fetch the data, render a minimal HTML template, then stream the PDF. Example with dompdf:

<?php
require __DIR__ . '/vendor/autoload.php';

use Dompdf\Dompdf;

$id = (int)($_GET['id'] ?? 0);
// TODO: fetch form record by $id from DB into $row

$html = '<h1>Submission</h1>'
      . '<p>Name: ' . htmlspecialchars($row['name']) . '</p>'
      . '<p>Email: ' . htmlspecialchars($row['email']) . '</p>';

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4');
$dompdf->render();
$dompdf->stream('submission.pdf', ['Attachment' => false]); // opens inline for Print

This approach gives you both Save as PDF (download) and Print (browser print button). dompdf and mPDF each have CSS limitations; start with simple, print-friendly HTML, and sanitize any user-provided content before passing it to the renderer. (github.com, mpdf.github.io)

broj1’s TCPDF suggestion is also solid if you prefer a PDF-first API, but for the most accurate modern CSS you can also shell out to wkhtmltopdf (or headless Chrome) which prints HTML pages to PDF with strong CSS support. Use it from PHP by generating the HTML and invoking the binary. (wkhtmltopdf.org)

Recommended Answers

All 3 Replies

You are probably best installing the PDF library for PHP:

then it's as simple as:

<?php

try {
    $p = new PDFlib();

    /*  open new PDF file; insert a file name to create the PDF on disk */
    if ($p->begin_document("", "") == 0) {
        die("Error: " . $p->get_errmsg());
    }

    $p->set_info("Creator", "hello.php");
    $p->set_info("Author", "Rainer Schaaf");
    $p->set_info("Title", "Hello world (PHP)!");

    $p->begin_page_ext(595, 842, "");

    $font = $p->load_font("Helvetica-Bold", "winansi", "");

    $p->setfont($font, 24.0);
    $p->set_text_pos(50, 700);
    $p->show("Hello world!");
    $p->continue_text("(says PHP)");
    $p->end_page_ext("");

    $p->end_document("");

    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=hello.pdf");
    print $buf;
}
catch (PDFlibException $e) {
    die("PDFlib exception occurred in hello sample:\n" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
    $e->get_errmsg() . "\n");
}
catch (Exception $e) {
    die($e);
}
$p = 0;
?>

taken from

please tell me where to write my php file name in below code whose content will show on pdf file.
Need suggestions.

You are probably best installing the PDF library for PHP:

then it's as simple as:

<?php

try {
    $p = new PDFlib();

    /*  open new PDF file; insert a file name to create the PDF on disk */
    if ($p->begin_document("", "") == 0) {
        die("Error: " . $p->get_errmsg());
    }

    $p->set_info("Creator", "hello.php");
    $p->set_info("Author", "Rainer Schaaf");
    $p->set_info("Title", "Hello world (PHP)!");

    $p->begin_page_ext(595, 842, "");

    $font = $p->load_font("Helvetica-Bold", "winansi", "");

    $p->setfont($font, 24.0);
    $p->set_text_pos(50, 700);
    $p->show("Hello world!");
    $p->continue_text("(says PHP)");
    $p->end_page_ext("");

    $p->end_document("");

    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=hello.pdf");
    print $buf;
}
catch (PDFlibException $e) {
    die("PDFlib exception occurred in hello sample:\n" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
    $e->get_errmsg() . "\n");
}
catch (Exception $e) {
    die($e);
}
$p = 0;
?>

taken from

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.