0

I know: opinion-based stuff shouldn't be asked etc., but this isn't about opinion, but for now simply about what still exists or rather what will still work today.

My concern: I am looking for a solution to generate one-page pdf files from PHP/HTML pages that get their content from a database and are rather heavily styled with CSS (also including tabular data and images). A function that lets you open or download the PDF when clicking on a link. The PDF should just basically look the same as the corresponding webpage at size A4 (I'll style it that way). As if you choose "preview/save as PDF" in MacOS' printing dialog, but without the user needing any particular software, working on any OS and browser.

I searched SO and the web, and I found a lot of old posts and pages (3 years and much older), like Convert HTML + CSS to PDF with PHP? , Generate PDF report from php and Generate PDF from HTML PHP I can't see in these posts if any of this is still up-to-date / working.

So I'd have to download all that stuff and build it into my pages, maybe only find out that it doesn't work anymore or isn't really applicable for my situation.

Could people who have experience with that kind of stuff please point me to places where I can find scripts/libraries which are able to do this and work with PHP 5.6 and 7? It doesn't have to support CSS3, I can restrict these pages to CSS2, and although I am using webfonts on that website, I can get along without them for the PDFs. Possibly for free, but also a not-too-expensive commercial solution would be okay. I'd be very grateful for any help.

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • 1
    I didn't vote down, nor voted for a thread close. I do agree that there are many threads that went through this. Anyway my experience: Most PHP based Html to PDF convertors supporting css styling to a certain degree work well... There aren't many open source up to date solutions and modern css3 styles aren't supported. You can generate basic PDF files from HTML, but you wont have a luck with very specific styles. – Adam K. Mar 14 '17 at 14:23
  • [Also as reference for the downvote discuss](https://meta.stackoverflow.com/questions/285081/am-i-still-supposed-to-explain-my-downvotes-or-not), this could be also too broad or asking for libraries .... – DaniP Mar 14 '17 at 14:24
  • 1
    did you try dompdf? – plonknimbuzz Mar 14 '17 at 14:49
  • @plonknimbuzz No, I haven't come across that one yet - thanks! – Johannes Mar 14 '17 at 14:59
  • 1
    try that. that library is easier to use. you can convert your html (even with css) to pdf. Not all css supported yet, but you can hack them with your knowledge and experiment. Last, if you have a template which you want to fill it with other content, you should use fpdi (class extension of fpdf). i use both library for 5 years till now. hope this info will help you. – plonknimbuzz Mar 15 '17 at 07:25

1 Answers1

0

You can use the mpdf library. It's very easy to learn. Here is the sample code. It works perfectly.

You can get value from another page, using post method also. Your choice.

<?php $student_id = $_GET['student_id']; ?>
<?php
include("mpdf/mpdf.php");
$html .= "
<html>
<head>
<style>
body {font-family: sans-serif;
    font-size: 10pt;
    background-image: url(\"images/ok.jpg\");

    background-repeat: no-repeat;
    padding-top:10pt;
    margin-top: 100px;
    padding-top: 50px;
}
td { vertical-align: top; 
    border-left: 0.6mm solid #000000;
    border-right: 0.6mm solid #000000;
    align: center;
}

p.student_id{
    padding-left : 140px;
    padding-top  : -27px;
} 

</style>
</head>
<body>
<!--mpdf                                                                          

<p class=\"student_id\">$student_id</p>


<sethtmlpageheader name='myheader' value='on' show-this-page='1' />
<sethtmlpagefooter name='myfooter' value='on' />
mpdf-->


</body>
</html>
";

$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->SetDisplayMode('fullpage');

$mpdf->Output();
?>
Pang
  • 9,564
  • 146
  • 81
  • 122
Ashwani Garg
  • 1,479
  • 1
  • 16
  • 22