mPDF setAutoMargin not working for first page - php

The following code works perfectly for the second, third etc pages, but not for the first one.
/*
* Encoding
* Size (A4, etc)
* Font-size
* Font-type
* margin_left
* margin_right
* margin_top
* margin_bottom
* margin_header
* margin_footer
* Orientation
*/
$this->mPDF = new mPDF('utf-8', 'A4', 9, 'freesans', 10, 10, 0, 25, 5, 4, 'P');
$this->mPDF->setAutoTopMargin = 'pad';
$html = '<sethtmlpageheader name="myheader" value="on" show-this-page="1"></sethtmlpageheader>';
In the first page, the body content overlap the header content, while on the other pages the body content doesn't overlap the header content and fits perfectly below the header.
I want the same behavior on the first page, how do I do it?
EDIT 1:
An image to illustrate the problem I'm facing

I found out this answer http://www.mpdf1.com/forum/discussion/621/margin-top-problems/p1 which basically reports my issue.
The solution is to add a page and delete it so the code calculates the margins and then add again the page.
$this->mPDF->WriteHTML($html);
$this->mPDF->page = 0;
$this->mPDF->state = 0;
unset($this->mPDF->pages[0]);

Related

How to set custom page size in fpdf php library?

I need some help with fPDF. I want to set up my custom page size (exactly: width 3 inch, and height 5 or 6 inch).
it will create number of pages again height parameter .
i set the size array(3,5). it will create 5 page. I
found fPDF() manual (http://www.fpdf.org/) but there are only ready formats like A4, B5 etc. I have to set up my own page format.
<?php
require_once('fpdf/fpdf.php');
//$fromat = array(3,5);
$pdf = new FPDF('p','in', [4.1,2.9]);
$pdf->SetTopMargin(50);
$pdf->Addpage();
$pdf->SetTitle("invoice");
$pdf->SetCreator("maqbool solutons");
$pdf->SetAuthor("my name");
$pdf->SetSubject("report");
$pdf->SetFont('Arial', 'B', '16');
$pdf->SetTextColor(155,14,9);// rgb
$pdf->SetDrawColor(155,14,9);
$pdf->SetfillColor(15,140,95);
$pdf->Cell(60,10, 'hello word');
$pdf->Cell(60,10,'powered by fpdf', 1, 0,'c',true);
$pdf->Cell(60,10,'powered by fpdf', 1, 2,'c');
$pdf->Cell(60,10,'powered by fpdf', 1, 1,'c');
$pdf->Image("images/coat.jpg", 10,20,10,35);
$pdf->MultiCell(94,10,"skldjfsldfsfjsdkfsjdlfjsdflkjsdflksjflksjdflskjfslkjfdslkfdjslkfdjslkfjslkfjslkfjsflkjsflkjsflksjflksjfslkjfslkjslkf",1,"L",false);
$pdf->Output("I", "invice.pdf");
?>[that is my file size][1]
when i add array of size
You should should define it in your constructor like so:
$pdf = new FPDF('P','in',[3,6]);
You can find more info in tutorial #1 and in the manual > AddPage
As said in the documentation, when you call the constructor or AddPage, you can either give a String or an Array containing the width and height:
// AddPage([string orientation [, mixed size [, int rotation]]])
$pdf->AddPage("P", [3, 5]); // assuming you are using 'in' as unit
Or directly using the constructor:
// __construct([string orientation [, string unit [, mixed size]]])
$pdf = new FPDF('P','in',[3, 5]);
I think you can set the page size with the constructor.
I have not tested it but this should show you the way:
$format=array(3,5);
$pdf=new FPDF('P','in',$format);
$pdf->Open();
....

How to make pdf exact 10CM width with DOMPDF and Laravel?

I am creating a PDF with DOMPDF and laravel.
The pdf is being printed with a special printer that only accepts files with 10CM width and 20CM height.
I have tried this:
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper);
Since 11X17 is
"11x17" => array(0, 0, 792.00, 1224.00),
I figured 10X20 was 0,0720,1440
But it's not working.
Any help is appreciated.
Thanks in advance
How i fixed this:
change the custom paper.
Download the PDF open in Acrobat Reader move your mouse to the left corner now you can see the width and height of the document, and i changed the custom paper accordingly.
The end result was:
10CM X 20CM =
$customPaper = array(0,0,567.00,283.80);
$pdf = PDF::loadView('pdf.retourlabel', compact('retour','barcode'))->setPaper($customPaper, 'landscape');
Very circuitous work but i did get the Job done..
Thanks
Setting the paper size in PHP requires knowing the point (pt) measurement, points being the native PDF unit. The PDF resolution (with Dompdf) is 72pt/inch. So 10cm x 20cm is roughly equivalent to 283 X 566 (as you noted in your answer).
1 inch = 72 point
1 inch = 2.54 cm
10 cm = 10/2.54*72 = 283.464566929
20 cm = 10/2.54*72 = 566.929133858
landscape is mean opposite. So, we can set it like : array(0, 0, 566.929133858, 283.464566929 ) which same as the answer but in more precise value
You can, however, allow Dompdf to calculate the appropriate point size by specifying your page dimensions in CSS. This is available starting with Dompdf 0.6.2.
<html>
<head>
<style>
#page { size: 10cm 20cm landscape; }
</style>
</head>
<body>
...
</body>
</html>
Trivia: the PDF spec does not provide for a method of indicating paper orientation (though there is a method of indicating a rotation). Dompdf just flips the width/height when landscape orientation is specified.
I think the issue is with orientation, since the setPaper uses 'A4' orientation as default so this might be the reason that your code is not working.
/**
* Set the paper size (default A4)
*
* #param string $paper
* #param string $orientation
* #return $this
*/
public function setPaper($paper, $orientation = 'portrait'){
$this->paper = $paper;
$this->orientation = $orientation;
$this->dompdf->setPaper($paper, $orientation);
return $this;
}
Try using :
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper,'portrait');
Hope that helps or try other options too instead of portrait.

Some of my PHP string disappears when being passed into SQL

I've created a Wordpress plugin that detects when a post is published and creates a topic in a phpBB installation.
As part of the plugin, the Wordpress post's excerpt is passed to the phpBB SQL database as the post text, followed by a few line breaks and a 'read more' link that is a permalink to the Wordpress post.
The excerpt + link is created as follows:
$wparticle_excerpt = get_the_excerpt()."<br /><br />Click here to read the full article →";
Then it's escaped:
$wparticle_excerpt = $con->real_escape_string( $wparticle_excerpt );
And is then written into the SQL database using the following query:
$sql_write_post = "INSERT INTO phpbb_posts (
topic_id,
forum_id,
poster_id,
icon_id,
post_time,
post_approved,
post_reported,
enable_bbcode,
enable_smilies,
enable_magic_url,
enable_sig,
post_subject,
post_text,
post_checksum,
post_attachment,
post_postcount,
post_edit_time,
post_edit_user,
post_edit_count,
post_edit_locked
) VALUES (
/* topic_id */ $new_topic_id,
/* forum_id */ $forum_to_post_in,
/* poster_id */ $userid_to_assign,
/* icon_id */ 0,
/* post_time */ $date_to_assign,
/* post_approved */ 1,
/* post_reported */ 0,
/* enable_bbcode */ 0,
/* enable_smilies */ 0,
/* enable_magic_url */ 1,
/* enable_sig */ 0,
/* post_subject */ '$wparticle_title',
/* post_text */ '$wparticle_excerpt',
/* post_checksum */ '$wparticle_excerpt_md5',
/* post_attachment */ 0,
/* post_postcount */ 1,
/* post_edit_time */ 0,
/* post_edit_user */ 0,
/* post_edit_count */ 0,
/* post_edit_locked */ 0
)";
if (!mysqli_query($con,$sql_write_post)) { die ("Error while creating post: " . mysqli_error($con)); }
The problem is that when I check the output, all I get are the two line breaks and the read more link. The actual excerpt, the part created by get_the_excerpt(), is completely gone. All I get is this:
<br />
<br />
Click here to read the full article →
I've dumped the $wparticle_excerpt variable after escaping it, and I know it's good. It's just one long string.
If I put any text in front of the excerpt, that stays, as long as it's hard-coded in PHP. Then there's a blank space where the excerpt should be, then the line breaks. No matter what I do I can't keep the excerpt there, it just disappears somewhere.
I'm sure this is the most simple thing but I'm mostly self-taught and I have no idea how or why this is happening. Any help would be incredibly appreciated. Please let me know if I haven't explained something properly.
The phpbb_posts table I'm writing to is utf8_bin and the post_text field in there is mediumtext type and utf8_bin, if this is of any help.

TCPDF table of contents and issues with page numbers

I'm using the TCPDF library to generate PDFs with PHP/HTML. The docs can be found here: http://www.tcpdf.org/doc/code/classTCPDF.html.
I am trying to create a table of contents (as demonstrated in example 59 on the TCPDF site) but I'm experiencing a few issues:
1) The Table of Contents page number is showing as the last page of the document. (8 of 8 when in reality it's 2 of 8, comes after a cover page).
2) The page numbers on the other pages are not adjusting. The page after the TOC should be 3 of 8, but instead says 2 of 8.
3) The table of contents has the correct page numbers for the bookmarks, but these numbers don't match the page numbers on those pages (related to issue #2).
How I'm generating bookmarks:
I'm generating bookmarks for each page by calling the Bookmark() method after each added page.
$pdf->AddPage();
$pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
How I'm generating the table of contents:
This is ripped directly from Example 59 linked above. In that example, the scenario is slightly different as it does not have a cover page.
// add a new page for TOC
$pdf->addTOCPage();
// write the TOC title and/or other elements on the TOC page
$pdf->SetFont('times', 'B', 16);
$pdf->MultiCell(0, 0, 'Table Of Content', 0, 'C', 0, 1, '', '', true, 0);
$pdf->Ln();
$pdf->SetFont('helvetica', '', 10);
// define styles for various bookmark levels
$bookmark_templates = array();
/*
* The key of the $bookmark_templates array represent the bookmark level (from 0 to n).
* The following templates will be replaced with proper content:
* #TOC_DESCRIPTION# this will be replaced with the bookmark description;
* #TOC_PAGE_NUMBER# this will be replaced with page number.
*
* NOTES:
* If you want to align the page number on the right you have to use a monospaced font like courier, otherwise you can left align using any font type.
* The following is just an example, you can get various styles by combining various HTML elements.
*/
// A monospaced font for the page number is mandatory to get the right alignment
$bookmark_templates[0] = '<table border="0" cellpadding="0" cellspacing="0" style="background-color:#EEFAFF"><tr><td width="155mm"><span style="font-family:times;font-weight:bold;font-size:12pt;color:black;">#TOC_DESCRIPTION#</span></td> <td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:12pt;color:black;" align="right"> #TOC_PAGE_NUMBER#</span></td></tr></table>';
$bookmark_templates[1] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="5mm"> </td><td width=" 150mm"><span style="font-family:times;font-size:11pt;color:green;">#TOC_DESCRIPTION#</span></td><td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:11pt;color:green;" align="right">#TOC_PAGE_NUMBER#</span></td></tr ></table>';
$bookmark_templates[2] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="10mm"> </td><td width=" 145mm"><span style="font-family:times;font-size:10pt;color:#666666;"><i>#TOC_DESCRIPTION#</i></span></td><td width="25mm "><span style="font-family:courier;font-weight:bold;font-size:10pt;color:#666666;" align="right">#TOC_PAGE_NUMBER#</span ></td></tr></table>';
// add other bookmark level templates here ...
// add table of content at page 1
// (check the example n. 45 for a text-only TOC
$pdf->addHTMLTOC(2, 'INDEX', $bookmark_templates, true, 'B', array(128,0,0));
// end of TOC page
$pdf->endTOCPage();
How I'm getting the page numbers for the footers:
// Page footer
public function Footer() {
$pageN = $this->PageNo();
if($pageN === 1){
// Do nothing
} else {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont($helvetica_light, '', 10);
$this->setTextColor(255,255,255);
// Page number
$this->Cell(0, 12, ' '.$this->PageNo().' of '.$this->getNumPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
Basically, I'd like the Table of Contents to fall on page 2, with a footer that says Page 2, all subsequent page numbers to be labeled correctly. How can I do this? I can clarify the question/code if needed.
Any help is appreciated :)
PageNo() is a bit funky when it comes to calculating pages. It outputs what page it is in order of creation, and since the ToC page is created as the very last it will show that as the last page.
So instead use
// Page number
$this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
The rest of the code you provided, like the check if it is the first page, should work with PageNo().

TCPDF 2 columns same amount of text?

Hi I'm using tcpdf to generate a dynamic pdf for an e-zine i work for. But i have a problem, When i write the body text, I need to have the same amount of text per column and i use 2 of them. It writes my html text perfectly including images and everything, but at the end it doesn't align the text of both columns...
I'm new to tcpdf and i don't have any ideas where to start looking for a solution... any ideas how can i do this?
I'm using this function to write the text, a correction or a mockup for a solution would be greatly appreciated.
/**
* Print chapter body
* #param $file (string) name of the file containing the chapter body
* #param $mode (boolean) if true the chapter body is in HTML, otherwise in simple text.
* #public
*/
public function PartBody($text, $mode=false) {
$this->selectColumn();
// get esternal file content
$content = $text;
// set font
$this->SetFont('times', '', 9);
$this->SetTextColor(50, 50, 50);
// print content
if ($mode) {
// ------ HTML MODE ------
$this->writeHTML($content, true, false, true, false, 'J');
} else {
// ------ TEXT MODE ------
$this->Write(0, $content, '', 0, 'J', true, 0, false, true, 0);
}
$this->Ln();
}

Categories