joomla: How to force a php webpage to save as a pdf file
As an example, I have actually been able to generate excel files by changing the content-type for an html document. In this scenario, I would use php to generate the table cells with information gathered from the database and then set the content-type before the page is rendered. Is th开发者_StackOverflow社区ere a way to do this for pdf's?
Easiest way to generate PDF is by using DOMPDF. Learn more about DomPDF on Google's Projects.
It is extremely simple:
- Generate HTML page
- Make a call to DomPDF
- Download/Save your PDF
Here is demo showing how easy and fast it is.
Because you are using Joomla you can create PDF view.
Create view called view.pdf.php
in the view directory, include template and PDF will be generated automatically. Unfortunately Joomla's PDF is very plain... just text, images and other basic elements.
have actually been able to generate excel files by changing the content-type for an html document.
Well, not really: You have been building a HTML file, that Excel happens to be able to parse, and wrongly declaring it as a native Excel file. (I mean no criticism with this - the method works fine within its limitations - but to be exact. :)
As far as I know, this is not possible to do for PDF. You'll have to generate a PDF file using a library, or send the page to a PDF printer on the client's side.
If you have a simple HTML then I suggest you use TCPDF to generate your PDF output. Just check out example 6 and 61 on the TCPDF examples page to see how easy it is to generate PDFs based on a subset of HTML.
You need to use a library to generate the PDF document. You can't just change the content-type like you did for Excel.
Try http://www.fpdf.org/
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
As well an fpdf there's also tcpdf which is worth checking out.
Also, if you are interested in generating actual Excel files then have a look at PHPExcel.
精彩评论