1

I am creating a PDF using FPDF. The PDF renders to the web page fine, 95% of the time. Occasionally, the PDF will render to the page with all the text characters aligned left, but stacked on top of each other. As if the letter spacing is set to zero!

When I right-click on the web page and click "Print" from the "Right-Click" menu and print the PDF, the same thing happens, but ALL OF THE TIME..... However, when I save the PDF to my desktop, and open it to print, the PDF will print normal ALL OF THE TIME! Why?? I have uninstalled, then reinstalled my printer\drivers twice. I have not tried to print from a different printer/computer... Also, I do have a backup copy that I know did not have this problem from December 18th, 2017 and it still does this with that copy as well.....

See image stacked characters

    date_default_timezone_set('America/Phoenix');
    require('fpdf/fpdf.php');

    class PDF_receipt extends FPDF {
    function __construct ($orientation = 'L', $unit = 'pt', $format = 'Letter', 
    $margin = 33) {
    $this->FPDF($orientation, $unit, $format);
    $this->SetTopMargin($margin);
    $this->SetLeftMargin($margin);
    $this->SetRightMargin($margin); 
    $this->SetAutoPageBreak(true, $margin);
    }

// HEADER FUNCTION
function Header() {
    $this->SetFont('Arial', '', 16);
    $this->SetXY(30, 15);
    $this->SetFillColor(232, 249, 169);
    $this->SetTextColor(100);
    $this->Cell(0, 30, "MONKEYPOD KITCHEN ONLINE ORDERING", 0, 1, 'C', true);
    $this->Image("images/ttdinfocopy.jpg",40,55,225,60,"jpg", "");
    $this->Image("images/mens-ladies-keiki.jpg",535,58,200,42,"jpg", "");
}

// ORDER SUBMIT DATE FUNCTION
function SubmitDate() {   
        $this->SetFont('Arial', '', 11);
        $this->SetXY(150, 252);
        $this->Cell(222, 20, 'Order Date: ' . date('F jS Y g:i:s A') . ' AZ time', 0, 1);
}

// TABLE CONSTRUCTION FUNCTION
function QtyTable() {
    $this->SetFont('Arial', '', 9);
    $this->SetTextColor(0);
    $this->SetFillColor(240);
    $this->SetLineWidth(1);
    $this->SetXY(30, 242);  
    $this->Cell(100, 10, "", '0', 0, 'C', false);
    $this->Cell(300, 10, "", '0', 0, 'C', false);
    $this->Cell(95, 10, "", '0', 0, 'C', false);    
    $this->Cell(28, 10, "", '0', 0, 'C', false);    
    $this->Cell(28, 12, "3-6", 'LTR', 0, 'C', true);    
    $this->Cell(28, 12, "6-12", 'LTR', 0, 'C', true);
    $this->Cell(28, 12, "12-18", 'LTR', 0, 'C', true);
    $this->Cell(28, 12, "18-24", 'LTR', 0, 'C', true);
    $this->Cell(28, 10, "", '0', 0, 'C', false);
    $this->Cell(28, 10, "", '0', 0, 'C', false);
    $this->Cell(38, 10, "", '0', 0, 'C', false);        
    $this->Ln(10);
    $this->Cell(97, 10, "", '0', 0, 'C', false);
    $this->Cell(300, 10, "", '0', 0, 'C', false);
    $this->Cell(95, 10, "", '0', 0, 'C', false);    
    $this->Cell(28, 10, "", '0', 0, 'C', false);    
    $this->Cell(28, 10, "Month", 'LR', 0, 'C', true);   
    $this->Cell(28, 10, "Month", 'LR', 0, 'C', true);
    $this->Cell(28, 10, "Month", 'LR', 0, 'C', true);
    $this->Cell(28, 10, "Month", 'LR', 0, 'C', true);
    $this->Cell(28, 10, "", '0', 0, 'C', false);
    $this->Cell(28, 10, "", '0', 0, 'C', false);
    $this->Cell(38, 10, "", '0', 0, 'C', false);        
    $this->Ln(10);  
    $this->Cell(97, 20, "", '0', 0, 'C', false);
    $this->Cell(300, 20, "", '0', 0, 'C', false);
    $this->Cell(95, 20, "", '0', 0, 'C', false);    
    $this->Cell(28, 20, "2", 'LTR', 0, 'C', true);  
    $this->Cell(28, 20, "4", 'LTR', 0, 'C', true);  
    $this->Cell(28, 20, "6", 'LTR', 0, 'C', true);
    $this->Cell(28, 20, "8", 'LTR', 0, 'C', true);
    $this->Cell(28, 20, "10", 'LTR', 0, 'C', true);
    $this->Cell(28, 20, "12", 'LTR', 0, 'C', true);
    $this->Cell(28, 20, "O/S", 'LTR', 0, 'C', true);
    $this->Cell(38, 20, "", '0', 0, 'C', false);        
    $this->Ln(18);  
    $this->Cell(97, 20, "Item Name", 'LTR', 0, 'C', true);
    $this->Cell(300, 20, "Description", 'LTR', 0, 'C', true);
    $this->Cell(95, 20, "Color", 'LTR', 0, 'C', true);  
    $this->Cell(28, 20, "XS", 'LTR', 0, 'C', true); 
    $this->Cell(28, 20, "SM", 'LTR', 0, 'C', true); 
    $this->Cell(28, 20, "MD", 'LTR', 0, 'C', true);
    $this->Cell(28, 20, "LG", 'LTR', 0, 'C', true);
    $this->Cell(28, 20, "XL", 'LTR', 0, 'C', true);
    $this->Cell(28, 20, "XX", 'LTR', 0, 'C', true);
    $this->Cell(28, 20, "XXX", 'LTR', 0, 'C', true);
    $this->Cell(38, 20, "Total", 'LTR', 1, 'C', true);
}

function MessageBottom() {
        $message = "Thank you for your online order. If you have any questions, you can email us at the following address: ";
        $this->SetFont('Arial', '', 10);
        $this->SetFillColor(242);
        $this->SetXY(35, 560);
        $this->Write(12, $message);
}

function EmailAddress() {
        $this->SetFont('Arial', 'U', 10);
        $this->SetTextColor(1, 162, 232);
        $this->SetXY(488, 559.5);
        $this->Write(12, "orders@taketwodesigns.com", "mailto:orders@taketwodesigns.com?subject=Question...&body=link to PDF: http://taketwodesigns.com/monkeypod/php-pdf-practice/retrieve_order.php");
}

function Footer() {
    $this->SetFont('Arial', '', 10);
    $this->SetTextColor(0);
    $this->SetXY(30, 575);  
    $this->Cell(0, 35, "Take Two Designs", 'T', 0, 'C');
}

//   KA'ANAPALI ITEMS BELOW
//   ITEM 1
function Item1() {

if(isset($_POST['item1Qty'])) {
    $item1name  = $_POST['item1name'];
    $item1desc  = $_POST['item1desc'];
    $item1color = $_POST['item1color'];
    $item1Qty   = $_POST['item1Qty'];        
    $this->SetFont('Arial', '', 10);
    $this->SetFillColor(242);
    $this->SetLineWidth(1); 
    $this->SetXY(33, 302);        
        $this->Cell(97, 20, $item1name, 1, 0, 'L');
        $this->Cell(300, 20, $item1desc, 1, 0, 'L');
        $this->Cell(95, 20, $item1color, 1, 0, 'L');
    $this->Cell(28, 20, $item1Qty[0], 1, 0, 'C');
        $this->Cell(28, 20, $item1Qty[1], 1, 0, 'C');
        $this->Cell(28, 20, $item1Qty[2], 1, 0, 'C');
        $this->Cell(28, 20, $item1Qty[3], 1, 0, 'C');
        $this->Cell(28, 20, $item1Qty[4], 1, 0, 'C');
        $this->Cell(28, 20, $item1Qty[5], 1, 0, 'C');
        $this->Cell(28, 20, '', 1, 0, 'C');
        $this->Cell(38, 20, array_sum($item1Qty), '1', 0, 'C');
} 
}

// CREATE PDF PAGE
    $pdf = new PDF_receipt();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 12);

// GREY RECTANGLES CREATED TO FILL PDF FIELD BACKGROUND COLOR
    $pdf->SetFillColor(242);
    $pdf->Rect(128, 125, 200, 100, 'F');
    $pdf->Rect(537, 125, 200, 100, 'F');

// ORDERED-BY TEXT
    $pdf->SetFont('Arial', '', 10);
    $pdf->SetXY(45, 125);
    $pdf->Cell(80, 20, "Customer PO:", 0, 2, 'R');
    $pdf->Cell(80, 20, "Cancel Date:", 0, 2, 'R');
    $pdf->Cell(80, 20, "Contact Phone:", 0, 2, 'R');
    $pdf->Cell(80, 20, "Buyer:", 0, 2, 'R');
    $pdf->Cell(80, 20, "Email:", 0, 2, 'R');

// ORDERED-BY INFO BOX
    $pdf->SetFont('Arial', '');
    $pdf->SetXY(128, 125);
    $pdf->Cell(200, 20, $_POST['ponum'], 1, 2);
    $pdf->Cell(200, 20, $_POST['canceldate'], 1, 2);
    $pdf->Cell(200, 20, $_POST['contactphone'], 1, 2);
    $pdf->Cell(200, 20, $_POST['buyer'], 1, 2);
    $pdf->Cell(200, 20, $_POST['orderemail'], 1, 2);

// SHIP-TO TEXT
    $pdf->SetFont('Arial', '');  
    $pdf->SetXY(454, 125); 
    $pdf->Cell(80, 20, "Ship To/Bill To:", 0, 2, 'R');
    $pdf->Cell(80, 20, "Address:", 0, 2, 'R');     
    $pdf->Cell(80, 20, "City, State, Zip:", 0, 2, 'R'); 
    $pdf->Cell(80, 20, "Phone:", 0, 2, 'R');
    $pdf->Cell(80, 20, "Email:", 0, 2, 'R');

// SHIP-TO INFO BOX
    $pdf->SetFont('Arial', '');
    $pdf->SetXY(537, 125);
    $pdf->Cell(200, 20, ' Monkeypod Kitchen - Ka\'anapali', 1, 2);
    $pdf->Cell(200, 20, ' 1489 Ka\'anapali Pkwy.', 1, 2);
    $pdf->Cell(200, 20, ' Lahaina HI, 96742', 1, 2);
    $pdf->Cell(200, 20, ' 602-555-5555', 1, 2);
    $pdf->Cell(200, 20, ' bigd@ccccvvbb.net', 1, 2);

// ITEMS TABLE FUNCTION CALL
    $pdf->QtyTable();

// ORDER DATE FUNCTION CALL
    $pdf->SubmitDate();

// ITEMS FUNCTION CALLS        
    $pdf->Item1();
    $pdf->Item2();
    $pdf->Item3();
    $pdf->Item4();
    $pdf->Item5();
    $pdf->Item6();
    $pdf->Item7();
    $pdf->Item8();
    $pdf->Item9();
    $pdf->Item10();
    $pdf->Item11();

// BOTTOM MESSAGES FUNCTION CALLS
    $pdf->MessageBottom();
    $pdf->EmailAddress();

// OUTPUT NEW PDF TO FOLDER
    $pdf->Output('receipts/receipt_' . date("mdY_His") . '.pdf', 'F');

And the "retrieve file code"

date_default_timezone_set('America/Phoenix');

$dir = "receipts";         
$pattern = '/\.(zip|ZIP|pdf|PDF)$/'; // only check files with these extensions          
$newstamp = 0;            
$newname = "";

if ($handler = opendir($dir)) {               
    while (false !== ($fname = readdir($handler)))  { 

// Eliminate current directory, parent directory            
        if (preg_match('/^\.{1,2}$/',$fname)) continue; 

// Eliminate other pages not in pattern            
    if (! preg_match($pattern,$fname)) continue;            
        $timedat  = filemtime("$dir/$fname");            
    if ($timedat  > $newstamp) {
        $newstamp = $timedat; // $newstamp is the time for the latest file
        $newname  = $fname;   // $newname is the name of the latest file
    }
    }
}

closedir ($handler);    

// We'll be outputting a PDF
header('Content-Type: application/pdf');
header('Location: receipts/'.$newname);
Bigfootbud
  • 103
  • 1
  • 10
  • Please include the code you're using. There is more than likely a condition that is failing that would otherwise move the placement to the desired position(s). – Will B. Feb 09 '18 at 05:37
  • @fyrye I added code... – Bigfootbud Feb 09 '18 at 07:00
  • What's the viewer application? Any chance to get direct access to the URL which executes the code so that we can see if we can reproduces this? – Jan Slabon Feb 09 '18 at 07:17
  • Please include your other `Item**()` methods. Also which version of FPDF are you using? It looks like you are setting the value positions based on user supplied data that may not exist, dependant on different user actions. Which explains why 95% of the time rendering is fine. We can't troubleshoot the rendering issue without more info. However the retrieval appears to be invalid, since you are defining a content-type for a redirect. It should be [something like this](https://stackoverflow.com/a/20080402/1144627) or just `header('Location: /URI');` – Will B. Feb 09 '18 at 16:38
  • @Jan Slabon Here is a link to the prototype... Please try to print a copy... see how it looks... maybe try a couple. http://taketwodesigns.com/monkeypod/php-pdf-practice/prototype/2-10-18/ – Bigfootbud Feb 15 '18 at 02:33
  • Not unless you'd answered all other open questions. – Jan Slabon Feb 15 '18 at 11:35
  • Well, It does print fine from printers connected to other computers so it must be something new with my computer... Thanks for the help, though! :) – Bigfootbud Feb 22 '18 at 17:19
  • UPDATE... it must have been a Windows issue... after some Windows updates, the problem went away... Thanks for any help! :) – Bigfootbud Mar 23 '18 at 05:36

0 Answers0