[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2029
  • Last Modified:

Creating a PDF via PHP and having sizing problems $mpdf

Hi Guys,
I'm trying to create a business card size PDF document.
My problem is that the content becomes small rather than fill the 3.5inch  x 2inch PDF page.  The content is just shrinking !!!
Does anybody know how to fix this problem?
Sas

PS: I also want it to either echo a "PDF Created" message or forward to a page, but that won't function either. But I may have to open another ticket for that.


<?php
$pDate = date("m/d/Y");
$var1 = 'Company Address';

$content = '
<CENTER>
<table border=2  width="100%" height="100%" > <! ... without this outer table each element of the inner table becomes a seperate page ... -->
<TR><TD>
 <!-- .... CONTENT .... -->
<table border=0  width="100%" height="100%" >
<TR><TD><center><img src="http://www.CompanyName.com/images/Logo_250.png" width=250 border=0 alt="Company Name"></center>
</TD></TR>
<TR><TD style="font-size:24px;font-family:ariel;"><center>GUEST CARD</center>
</TD></TR>
<TR><TD style="font-size:19px;font-family:ariel;"><center><B>'.$var1.'</B></center>
</TD></TR>
<TR><TD style="font-size:14px;font-family:ariel;"><center>Issue Date: '.$pDate.'</center>
</TD></TR>
<TR><TD align=right><BR>&nbsp;<BR><img src="http://www.ServicedBy.com/images/Logo_130.png" width=130 border=0 alt="ServicedBy.com">
</TD></TR></TABLE>
</TD></TR></TABLE>
</CENTER>
';

//==============================================================
//==============================================================
include("mpdf.php");

$mpdf = new mPDF('utf-8', array(87,50));

// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text

$mpdf->WriteHTML($content);
$mpdf->Output("GuestCard.pdf","D");  // Use D to output to the browser and force a download. 

echo '<meta http-equiv="refresh" content="0;url=http://www.ExpertsExchange.com/">'; // For some unknown reason this does not forward.

exit;
//==============================================================
//==============================================================
?>

Open in new window

0
sasnaktiv
Asked:
sasnaktiv
  • 3
  • 2
2 Solutions
 
Ray PaseurCommented:
I can show you how I use FPDF (see www.fpdf.org for more) and I have found it to be lightning fast once you get it set up.  Here is the "hello world" exercise.

<?php // RAY_fpdf_hello_world.php
error_reporting(E_ALL);


// DEMONSTRATE THE BASICS OF FPDF


// SOME VARIABLES FOR OUR TESTS (COULD COME FROM DATA BASE, ETC)
$font = 'Arial';
$text = 'Hello World!';

// BRING IN THE PDF THING
require_once('fpdf16/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= DIRECTORY_SEPARATOR
. 'RAY_junk'
. DIRECTORY_SEPARATOR
. 'temp_pdf_blue'
. '.pdf'
;
$pdf_file_name
= getcwd()
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A BLUE BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(  0,   0, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Blue PDF</a>';



// START OVER FOR A NEW PDF
unset($pdf);
echo PHP_EOL . "<br/>";

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= DIRECTORY_SEPARATOR
. 'RAY_junk'
. DIRECTORY_SEPARATOR
. 'temp_pdf_red'
. '.pdf'
;
$pdf_file_name
= getcwd()
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A RED BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(255,   0,   0);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Red PDF</a>';

Open in new window

0
 
sasnaktivAuthor Commented:
Thanks Ray,
I'll kick this around in the morning. I've already tried  www.fpdf.org but had trouble making it work with tables & variables. That's why I turned to mPDF53, which allowed me to incorporate the tables & variables but now it's giving me the size issue.
Sas
0
 
lwadwellCommented:
mPDF and the table are resizing due to the allowed space - with margins etc. taken into consideration for the page size.  You should set custom margins to use more of the business card.  e.g.
$mpdf = new mPDF('utf-8', array(87,50), 
		 0,     // font size - default 0
		 '',    // default font family
		 1,     // margin_left
		 1,     // margin right
		 1,     // margin top
		 1,     // margin bottom
		 1,     // margin header
		 1);    // margin footer;

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sasnaktivAuthor Commented:
Hi guys,
Just an update. I'm still working on solving this.

I've been trying to make this thing function the way I want it to with limited success.
The input from lwadwell was helpful in restricting the size of the pdf page, but other problems are created.

Ray's input is a bit more complex and I'm having trouble executing it with my variables.

I need some time to work on these and I will post my accomplishments or failures as soon as I possibly can.

Thanks for all the help,
Sas
0
 
sasnaktivAuthor Commented:
I'm not quite sure what's working & what's not.
 lwadwell's solution works with the code I submitted.
Ray's is an entirely different approach which I have not been able to master.

So I'll be awarding the points the best way I can in order to be fair.

And since I've been pushing this, I've run into other challenges. But I think I'll have to open another ticket to address them.
Thanks for the help.
Sas
0
 
Ray PaseurCommented:
Since this question, I have begun using TCPDF with very good results.  Maybe worth a look.  In any case, thanks for the points, ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now