从mysql数据库下载pdf

时间:2012-12-06 08:12:28

标签: php

  

可能重复:
  Create a PDF file with PHP

我需要帮助..我想要一个PHP代码来下载存储在mysql数据库中的信息并将其保存在pdf文件中。 到目前为止,下面是我的代码,但它不打印任何东西.. 请帮我修改它或给我发另一个更好的代码。 感谢..

<?php
$host = 'localhost';
$user = 'root';
$pass = 'hello';
$db = 'version';
$table = 'childinfo';
$file = 'export';
$csv_output;

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");


$gotten =mysql_query("select sex,childnumber from child_nfo");
$row =mysql_fetch_assoc($gotten);
$bytes = $row[imgdata];
header("Content-type: application/pdf");
header('Content-disposition: attachment; filename="thing.pdf"');
print $bytes;
?> 

2 个答案:

答案 0 :(得分:1)

您可以使用tcpdf创建pdf。尝试使用以下格式的tcpdf。除此之外,您还可以获得tcpdf文档以了解如何使用它。我正在共享代码,这只是一个如何使用tcpdf创建pdf文件的示例。

require_once('tcpdf/tcpdf.php');

        error_reporting(0);
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('your author name');
$pdf->SetTitle('title');
$pdf->SetSubject('subject');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('dejavusans', '', 10);

// add a page
$pdf->AddPage();

// writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='')
// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)

// create some HTML content
$html = $pdf_content;
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
//$pdf->Output('example_006.pdf', 'I');
$pdf_filename = time().".pdf";
$pdf->Output('destination_directory/'.$pdf_filename, 'F');

您可以将您在$pdf_content变量中获取的数据库信息。希望这能帮到你..

答案 1 :(得分:0)

我认为$ byes是图像的字节流?你不能只是print它并期望它是一个格式化的PDF文件。这些类可能在生成PDF时很有用:

FPDF:http://www.fpdf.org/

TCPDF:http://www.tcpdf.org/