用于将csv转换为pdf的php脚本

时间:2017-08-01 08:03:21

标签: php database pdf directadmin

我要修改下面的php脚本,使其以.pdf文件而不是.csv格式下载数据库。我该怎么办?目前,当调用此脚本时,数据库将作为.csv文件下载。数据库在directadmin中定义。

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require 'db.php';
define("tableStuff", "stuff");
define("tableUser", "user");

define("ERR", '{"status":1}');
define("INVALID", '{"status":2}');

define("rowUserId", "user_id");
define("rowRegDate", "reg_date");
define("rowStuffName", "stuff_name");
define("rowPurchaseDate", "purchase_date");
define("rowStuffCount", "stuff_count");
define("rowStuffDescription", "stuff_description");
define("rowPicUrl", "pic_url");
define("rowUserName", "user_name");

//**********************************************************************
$mysqli = mysqli_connect(DBIP, DBUN, DBPW, DBNAME);
if ($mysqli->connect_errno) {
    echo ERR;
    die;
}
mysqli_set_charset($mysqli, "utf8");
$result = $mysqli->query("SELECT ".rowStuffName.",".rowStuffCount.",".rowStuffDescription.",".rowPurchaseDate.",".tableStuff.".".rowRegDate.",".rowUserName." FROM " . tableStuff .",".tableUser.
    " WHERE " . tableStuff.".".rowUserId . " = " . tableUser.".".rowUserId);
if ($result->num_rows > 0) {
    $array = array();
    while ($row = $result->fetch_array(MYSQL_ASSOC)) {
        $array[] = $row;
    }
    header('Content-Encoding: UTF-8');
    header('Content-Type: application/csv; charset=utf-8' );
    header(sprintf( 'Content-Disposition: attachment; filename=stuff.csv', date( 'dmY-His' ) ) );
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    //echo pack("CCC",0xef,0xbb,0xbf);
    $title = array("نام کالا","تعداد (مقدار)","توضیحات","زمان ثبت","نام فرد ثبت کننده");
    $out = fopen("php://output", 'w');
    //fputs($out,"\xEF\xBB\xBF");
    fputcsv($out, $title,"\t");
    foreach ($array as $data)
    {
        fputcsv($out, $data,"\t");
    }
    fclose($out);
} else {
    echo INVALID;
}
mysqli_close($mysqli);

1 个答案:

答案 0 :(得分:0)

我来晚了,但是我正在使用FPDF来生成所有PDF文件,它包含一个内置示例,该示例使用CSV文件中的表格制作PDF,我只是更改了CSV文件的来源。这非常容易