下面是我的 php 脚本,它解析数据并将其返回到网页上。我试图将代码与 TCPDF 库集成,但我似乎无法让它工作。我想生成一个自动保存到我的桌面的 pdf 文件。这是我的代码:
<!--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");
?>
它在网页上生成的输出是这样的:
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