创建PDF.我该如何工作?错误填充 [英] Creating a PDF. How can I get this working? Errors populating

查看:91
本文介绍了创建PDF.我该如何工作?错误填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的php脚本,用于解析数据并将其返回到网页上.我试图将代码与TCPDF库集成,但似乎无法使其正常工作.我想生成一个pdf文件,该文件会自动保存到我的桌面上.这是我的代码:

Below is my php script that parses data and returns it on a webpage. I've attempted to integrate code with the TCPDF library, but I can't seem to get it to work. I'd like to generate a pdf file that automatically saves to my desktop. Here is my code:

<!--SETTING THE BACKGROUND FOR WEBPAGE -->

<?php
$bg = "bg-body.png";
?>

<html>
<style type="text/css">
body {
background-image: url('<?php echo $bg;?>');
background-repeat: repeat;
background-position: top center;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Movie List</title>
</html>

<!--PHP DATA PARSE FILE STARTS HERE -->

<?php
require_once("tcpdf/tcpdf.php");
$pdf = new TCPDF();

foreach (glob("*.mov") as $filename)

$theData = file_get_contents($filename) or die("Unable to retrieve file data");

$months = ['January' => '_01', 'February' =>  '_02', 'March' => '_03', 'April' => '_04', 'May' => '_05', 'June' => '_06', 'July' => '_07', 'August' => '_08', 'September' => '_09', 'October' => '_10', 'November' => '_11', 'December' => '_12'];
foreach($months as $key => $month){
  if(strpos($filename,$month)!==false){
        echo "<div style ='text-align: center; text-shadow: 0 .8px 0 #c4bc2a; margin-top: 30px; margin-bottom: 20px; font:16px verdana,tahoma,sans-serif;
                color:#6b8942; font-weight:bold; text-decoration: underline;'>Movie List for $key 2013</div>";
    }
}


$string = $theData;
$titles = explode("\n", $string);

function getInfo($string){
    $Ratings = ['G', 'PG', 'PG-13', 'R', 'NR', 'XXX'];
    $split = preg_split("/\"(.+)\"/", $string, 0, PREG_SPLIT_DELIM_CAPTURE); 
    if(count($split) == 3){ 
        preg_match("/(".implode("|", $Ratings).")\s/", $split[0], $matches);
        $rating = $matches[0];
        return ["title" => $split[1], "rating" => $rating];
    }
    return false;
}


$infolist = array();
foreach($titles as $title){
    $info = getInfo($title);
    if($info !== false){
    $infolist[] = $info;
    }
}

usort($infolist, "infosort");

function infosort($lhs,$rhs) {
  return strcmp($lhs['rating'], $rhs['rating']);
}

foreach ($infolist as $info) {
        echo "<div style ='margin-bottom: 3px; text-align: center;
          font:13px Verdana,tahoma,sans-serif;color:green;'> 
           {$info["title"]} : {$info["rating"]}</div>";
}

echo "<div style='text-align:center; margin-top: 20px;'><img src='shclogo.png'
alt='Logo' width='200' height='133'/></div>";

//PDF CODE STARTS HERE:

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("my name");
$pdf->SetTitle("my doc");
$pdf->SetSubject("x");
$pdf->SetKeywords("a, b, c");


// set default header data
$pic = "../shclogo.png";
$pdf->SetHeaderData(realpath($pic), "25", "Title");

// 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 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);

 //initialize document
 $pdf->AliasNbPages();

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

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

 // set font
 $pdf->SetFont("helvetica", "", 12);

 // set a background color
 $pdf->SetFillColor(230, 240, 255, true);


$pdf->SetFont("", "b", 16);
$pdf->Write(16, "{$info["title"]} : {$info["rating"]}\n", "", 0, 'C');

$pdf->Output("{$filename}.pdf", "I");
?>

它在网页上生成的输出是这样的:

The output it generates on the webpage is this:

Movie List for May 2013
Fat Burning Hip Hop Dance Party : G
Fat Burning Hip Hop Dance Grooves : G
Aladdin : G
Gullivers Travels : G
To the Arctic : G
(HD) The Package : NR
Americano : NR
Missing Brendan : NR
Point Doom : NR
Parental Guidance : PG
Life of Pi : PG
Chasing Mavericks : PG
(HD) Cirque du Soleil: Worlds Away : PG
The Little Princess : PG
(HD) Rise of the Guardians : PG
(HD) A Place at the Table : PG
(HD) Escape from Planet Earth : PG
Jack And The Beanstalk : PG
Taken 2 : PG-13
Cloud Atlas : PG-13
Jack the Giant Slayer : PG-13
(HD) Mama : PG-13
(HD) Safe Haven : PG-13
(HD) Les Miserables : PG-13
(HD) Jack Reacher : PG-13
(HD) Dark Skies : PG-13
(HD) The Guilt Trip : PG-13
The Hobbit: An Unexpected Journey : PG-13
(HD) Fun Size : PG-13
The Incredible Burt Wonderstone : PG-13
(HD) Identity Thief : PG-13
(HD) Shanghai Calling : PG-13
Beautiful Creatures : PG-13
(HD) The House at the End of the Street : PG-13
Adult title 2 : XXX
Adult title 1 : XXX

TCPDF ERROR: Some data has already been output, can't send PDF file

推荐答案

该错误告诉您问题出在哪里.在尝试发送PDF进行下载之前,您无法获得任何浏览器输出.

The error tells you what the problem is. You cannot have any browser output prior to trying to send the PDF to download.

删除所有HTML和任何回声,然后重试.

Remove all the HTML and any echo's and try again.

您甚至可能想要创建一个名为"download-pdf.php"的新文件-然后在其中包含PDF代码-然后在父文件中,您可以<a href="download-pdf.php">Download PDF</a>-单击后,它将发送该文件.

You may want to even create a new file called "download-pdf.php" - and then just have the PDF code in there -- then in the parent file, you can <a href="download-pdf.php">Download PDF</a> - upon click, it will send the file normally.

这篇关于创建PDF.我该如何工作?错误填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆