0

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ValleyDigital
  • 1,460
  • 4
  • 21
  • 37

1 Answers1

1

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

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

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.

Rob W
  • 9,134
  • 1
  • 30
  • 50