user1371896
user1371896

Reputation: 2230

converting extracted html content in to pdf and giving option to download using php

I have an html content stored in a variable.I have to generate a pdf file before giving it for download

like for example $content="test document containing test content"

Im trying to add this to a pdf file and give user the option to download it.

Ive tried the code

<?php 
header('Content-Type: text/pdf; charset=utf-8');

header('Content-Disposition: attachment; filename=data.pdf');

$output = fopen('output', 'w');

fputcsv($output, $content);
?>

This gives the option to download file as pdf, bt when I try opening it in pdf reader, it shows file type nt supported.. Is ther anyother way to do this in php??

Upvotes: 0

Views: 413

Answers (2)

Janis Peisenieks
Janis Peisenieks

Reputation: 4988

IF I understand what you're saying correctly, then you are just trying to output some html as a PDF file. That is not how it works. You need to generate a PDF, and then put this content in there.

I'd suggest taking a look at Zend_Pdf.

For downloading take a look here

Upvotes: 0

Romain
Romain

Reputation: 6420

If you already have a variable with your values, here $content, it's really quick to make it a PDF using FPDF (http://www.fpdf.org).

For example with your variables:

require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$content);
$pdf->Output();

Moreover, if you want your user to download directly the file, write:

$pdf->Output("Name.pdf","D");

Note that no information has to be sent before Output(); !

Upvotes: 1

Related Questions