I'm creating a PDF with a custom header and footer but, I don't understand why, when I add an image in the footer, the total page count gets busted and added twice:
$logoFileName = 'images/image.png';
$logoX = 0; // 0mm
$logoY = $this->GetY()-10; // 0mm
$logoWidth = 200; // 15mm
$logo = 'Pagina ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages() . $this->Image($logoFileName, $logoX, $logoY, $logoWidth);
// $this->SetX($this->w - $this->documentRightMargin - $logoWidth);
$this->Cell(0,10, $logo, 0, false, 'R', 0, '', 0, false, 'T', 'M');
Results:
first page
second page
With same script, if I delete this->image() the count is correct.
I also add that, if I remove the following function from the script:
$this->getAliasNbPages()
so it becomes so:
$logo = 'Pagina ' . $this->getAliasNumPage() . '/' . $this->Image($logoFileName, $logoX, $logoY, $logoWidth);
the first page is correct masculine but the following ones are correct:
first page
second page
Related
When I try to print variable "$ime", it just prints "Student: " on pdf file.
I have tested it on html and it works fine displaying whole thing as is should.
Sql result is not empty and it's just one row that displays name and last name. So normal output should be "Student: John Doe"
// Page header
function Header()
{
$db2 = new dbObj();
$connString2 = $db2->getConnstring();
mysqli_set_charset($connString2,'UTF-8');
$aaa = mysqli_query($connString2, "SELECT s.ime,s.prezime FROM Student s WHERE s.mbr='".$mbr."'") or die("database error:". mysqli_error($connString2));
$row=mysqli_fetch_row($aaa);
$ime ="Student: ".$row[0]." ".$row[1];
// Logo
$this->Image('img/logo.png', 85, 15, 30, '', 'PNG', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
$this->SetFont('Arial','B',13);
// Move to the right
$this->Cell(80);
// Title
$this->SetFont('Arial','B',15);
$this->Cell(20,80,'Lista polozenih ispita',0,0,'C');
$this->Ln(10);
$this->Cell(80);
$this->SetFont('Arial','B',10);
$this->Cell(20,80,$ime,0,0,'C');
// Line break
$this->Ln(50);
}
Edit: It's not about variable scoping I have tried but it doesn't affect it in any way.
hello please check pdf sample,
I'm using mpdf to generate a pdf document in php.
I got this pdf generated by the code below.
1st copy is perfect, but second copy of 1st page top header title show wrong i want to 2nd copy on header $title2
Can you help me?
<?php
include('mPDF/mpdf.php');
$mpdf = new mPDF('c', 'A4', '9', '', '10', '18', '55', '50', '50', '12', 'P');
$html = '
<html>
<head>
</head>
<body>';
for ($i = 1; $i < 80; $i++) {
$html .= '<div>Here is the text of the first chapter</div>';
}
$html .= '</body>
</html>';
$title1 = "<p style='text-align:right; margin:0px; font-size:15px;'>ORIGINAL </p>";
$title2 = "<p style='text-align:right; margin:0px; font-size:15px;'>DUPLICATE</p>";
$title3 = "<p style='text-align:right; margin:0px; font-size:15px;'>TRIPLICATE</p>";
$mpdf->SetHeader($title1 . $header);
$mpdf->SetFooter($footer . 'Page No. {PAGENO} of {nbpg}');
$mpdf->WriteHTML($html);
$mpdf->SetFooter($footer . 'Page No. {PAGENO} of {nbpg}');
$mpdf->WriteHTML('<pagebreak resetpagenum="1" pagenumstyle="1" suppress="" />');
$mpdf->SetHeader($title2 . $header);
$mpdf->SetFooter($footer . 'Page No. {PAGENO} of {nbpg}');
$mpdf->WriteHTML($html);
$mpdf->SetFooter($footer . 'Page No. {PAGENO} of {nbpg}');
$mpdf->Output();
PDF Sample
According to the documentation the SetHeader function can take three parameters, the third of which is $write.
This third parameter forces the Header to be written immediately to the current page. Use it if the header is being set after the new page has been added.
Change to:
$mpdf->SetHeader($title2 . $header, [], true);
and it should allow the second header to be seen.
You may also want to look at AddPage().
I'm trying to get some screenshot using php-driver. And it appears that despite taking the picture of the entire web page, it just taking the picture that appears on the monitor/screen (that's why we call it screenshot).
So my question is how to capture a picture that located in the bottom of the page? Do we scroll the page to the specified element? or there is a way to take the picture of the entire page?
This is my screenshot code:
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::firefox();
$webdriver = RemoteWebDriver::create($host, $capabilities, 5000);
function find_image($url) {
//Screenshot
$GLOBALS["webdriver"]->get($url);
$element = $GLOBALS["webdriver"]->findElement(WebDriverBy::cssSelector('#law > p > img'));
$element_width = $element->getSize()->getWidth();
$element_height = $element->getSize()->getHeight();
$element_x = $element->getLocation()->getX();
$element_y = $element->getLocation()->getY();
$screenshot = __DIR__ . "/number/" . count($GLOBALS["data"]) . ".png";
$GLOBALS["webdriver"]->takeScreenshot($screenshot);
$src = imagecreatefrompng($screenshot);
$dest = imagecreatetruecolor($element_width, $element_height);
imagecopy($dest, $src, 0, 0, $element_x, $element_y, $element_width, $element_height);
imagepng($dest, $screenshot);
return convert_image($screenshot);
}
As you say, you can't take a screenshot of anything more than is on the current screen. But you can scroll the window to the bottom of the page.
$this->webDriver->executeScript('window.scrollTo(0,document.body.scrollHeight);');
$this->webDriver->takeScreenshot("a.png");
I use an instance variable for the webDriver, using GLOBALS isn't a particularly good way of using it (IMHO).
You can pass it in as a variable, or use it as a parameter to a constructor if using a class.
I have a Wordpress blog and am trying to implement the foresight.js image script. In short, I need to target all post images, swap out the src, width, & height attributes with data-src, data-width, & data-height attributes. I then need to duplicate the image line and wrap it in <noscript> tags. This is the structure I'm trying to have Wordpress generate/create:
<img data-src="wordpress/image/url/pic.jpg" data-width="{get width of image with PHP & pass-in that value here} data-height="{get height of image with PHP and pass-in that value here}" class="fs-img">
<noscript>
<img src="wordpress/image/url/pic.jpg">
</noscript>
More info can be found at foresight.js' website.
I have searched the Wordpress codex and the best possible route I can find are to use filters (ie. 'get_image_tag' & 'image_tag') for modifying the html that Wordpress outputs for each image. I'm thinking that one of these options should work, or that I can do some pattern matching with regex (I know, not ideal), throw in a preg_replace and then inject this back into the_content filter.
I have tried some of these options but cannot get any to work. Could someone please offer some help?
'get_image_tag' attempt:
Found this particular one on the web, but it would need modified to fit my logic (see above structure)...can't make sense of what the preg_replace array is doing on my own.
<?php function image_tag($html, $id, $alt, $title) {
return preg_replace(array(
'/'.str_replace('//','\/\/',get_bloginfo('url')).'/i',
'/\s+width="\d+"/i',
'/\s+height="\d+"/i',
'/alt=""/i'
),
array(
'',
'',
'',
alt='"' . $title . '"'
),
$html);
}
add_filter('get_image_tag', 'image_tag', 0, 4);
?>
Another 'get_image_tag' attempt:
<?php function get_image_tag($id, $alt, $title, $align, $size='full') {
list($width, $height, $type, $attr) = getimagesize($img_src);
$hwstring = image_hwstring($width, $height);
$class = 'align' . esc_attr($align) . ' size-' . esc_attr($size) . ' wp-image-' . $id;
$class = apply_filters('get_image_tag_class', $class, $id, $align, $size);
$html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" data-width="' . $width . '" data-height="' . $height . '" class="' . $class . ' fs-img" />';
$html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size);
return $html;
}
?>
Pattern-matching attempt:
Tried creating my own regex on this one, but not sure if it's correct.
<?php function restructure_imgs($content) {
global $post;
$pattern = "/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)(|\")(.*?)>/i";
list($width, $height, $type, $attr) = getimagesize($2$3.$4$5);
$hwstring = image_hwstring($width, $height);
$replacement = '<img$1data-src=$2$3.$4$5 title="'.$post->post_title.'" data-width="'.$width.'" data-height="'.$height.'" class="fs-img"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'restructure_imgs');
?>
Unfortunately can't get any of these examples to work. Any help or sharing your pre-written scripts/functions would be much appreciated! Thanks for helping a student learn!!
Your approach is before the page rendering and i think is not impossible in wordpress, however you can do the same after the page was rendered with javascript.
I tried to do that with jQuery and ti seems to work:
<!-- Example image -->
<img src="http://fotografia.deagostinipassion.com/resources/photos/2/2n/ilCielo.JPG" width="200px" height="300px">
<!-- Import jquery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("img").wrap(function() {
$(this).wrap(function() {
var newimg = '<img data-src="' + $(this).attr('src') + '" data-width="' + $(this).attr('width') + '" data-height="' + $(this).attr('height') + '" class="fs-img">';
return newimg;
});
return '<noscript>';
});
});
</script>
If you need more help, ask me.
I've been struggling with the header and footer data for quite some time now and thought it was time to ask it here on the forum.
What I'm trying to do is decide that if a page is added if the header / footer should be added or not. so code-wise I want to set the header/footer to on or off when adding a page.
I've tried to manipulate the function AddPage by setting an extra argument $setFooterHeader which default is set to true. And then trying to set this argument to false whenever I do an addPage('','',false); but it ignores it for some reason and I can't figure out why.
If I set the default value of the argument to false in the function itself it works like a charm, but when I try to do it in my script and set it as an argument, it totally ignores it.
Here's a code snippet of the fpdf.php file (function addPage)
function AddPage($orientation='', $size='', $setHeaderFooter=true)
{
// Start a new page
if($this->state==0)
$this->Open();
$family = $this->FontFamily;
$style = $this->FontStyle.($this->underline ? 'U' : '');
$fontsize = $this->FontSizePt;
$lw = $this->LineWidth;
$dc = $this->DrawColor;
$fc = $this->FillColor;
$tc = $this->TextColor;
$cf = $this->ColorFlag;
if($this->page>0)
{
// Page footer
if ($setHeaderFooter == true)
{
$this->InFooter = true;
$this->Footer();
$this->InFooter = false;
// Close page
$this->_endpage();
}
}
// Start new page
$this->_beginpage($orientation,$size,$setHeaderFooter);
// Set line cap style to square
$this->_out('2 J');
// Set line width
$this->LineWidth = $lw;
$this->_out(sprintf('%.2F w',$lw*$this->k));
// Set font
if($family)
$this->SetFont($family,$style,$fontsize);
// Set colors
$this->DrawColor = $dc;
if($dc!='0 G')
$this->_out($dc);
$this->FillColor = $fc;
if($fc!='0 g')
$this->_out($fc);
$this->TextColor = $tc;
$this->ColorFlag = $cf;
// Page header
if ($setHeaderFooter == true)
{
$this->InHeader = true;
$this->Header();
$this->InHeader = false;
}
// Restore line width
if($this->LineWidth!=$lw)
{
$this->LineWidth = $lw;
$this->_out(sprintf('%.2F w',$lw*$this->k));
}
// Restore font
if($family)
$this->SetFont($family,$style,$fontsize);
// Restore colors
if($this->DrawColor!=$dc)
{
$this->DrawColor = $dc;
$this->_out($dc);
}
if($this->FillColor!=$fc)
{
$this->FillColor = $fc;
$this->_out($fc);
}
$this->TextColor = $tc;
$this->ColorFlag = $cf;
}
Below is a code snippet of my PHP script which uses FPDF
/** PHP FPDF */
require_once 'classes/FPDF/fpdf.php';
require_once 'classes/FPDI/fpdi.php';
class PDF extends FPDI
{
function Header()
{
$this->SetFont( 'Arial', 'B', 18 ); //set font to Arial, Bold, and 16 Size
//create heading with params
//0 - 100% width
//9 height
//"Page Heading" - With this text
//1 - border around it, and center aligned
//1 - Move pionter to new line after writing this heading
//'C' - center aligned
$this->Cell( 0, 9, 'Page Heading', 1, 1, 'C' );
$this->ln( 5 );
}
function Footer()
{
//move pionter at the bottom of the page
$this->SetY( -15 );
//set font to Arial, Bold, size 10
$this->SetFont( 'Arial', 'B', 10 );
//set font color to blue
$this->SetTextColor( 52, 98, 185 );
$this->Cell( 0, 10, 'Footer Text', 0, 0, 'L' );
//set font color to gray
$this->SetTextColor( 150, 150, 150 );
//write Page No
$this->Cell( 0, 10, 'Page No: ' . $this->PageNo(), 0, 0, 'R' );
}
}
// Create new PDF object
$pdf = new PDF('P','mm','A4');
$pdf->addPage('','',false);
// Output pdf file
$pdf->Output('test.pdf','D');
Your help is greatly appreciated!!
I have solved this issue by setting a flag outside the class and use this flag in the header and footer function
The fix is in the page section, not in the addPage function
Just before doing an $pdf->addPage You set the flag as addPage automatically calls the header and footer function.
Here's the correct code (snippet of PHP script which uses FPDF)
/** PHP FPDF */
require_once 'classes/FPDF/fpdf.php';
require_once 'classes/FPDI/fpdi.php';
class PDF extends FPDI
{
function Header()
{
if ($this->header == 1)
{
$this->SetFont( 'Arial', 'B', 18 ); //set font to Arial, Bold, and 16 Size
//create heading with params
//0 - 100% width
//9 height
//"Page Heading" - With this text
//1 - border around it, and center aligned
//1 - Move pionter to new line after writing this heading
//'C' - center aligned
$this->Cell( 0, 9, 'Page Heading', 1, 1, 'C' );
$this->ln( 5 );
}
}
function Footer()
{
if ($this->footer == 1)
{
//move pionter at the bottom of the page
$this->SetY( -15 );
//set font to Arial, Bold, size 10
$this->SetFont( 'Arial', 'B', 10 );
//set font color to blue
$this->SetTextColor( 52, 98, 185 );
$this->Cell( 0, 10, 'Footer Text', 0, 0, 'L' );
//set font color to gray
$this->SetTextColor( 150, 150, 150 );
//write Page No
$this->Cell( 0, 10, 'Page No: ' . $this->PageNo(), 0, 0, 'R' );
}
}
}
// Create new PDF object
$pdf = new PDF('P','mm','A4');
$pdf->header = 0;
$pdf->footer = 0;
$pdf->addPage('','',false);
// Output pdf file
$pdf->Output('test.pdf','D');
I know you found out the awnser already for yourself, but as one of the commenters pointed out this didn't work for me with the footer.
The good news is you can do without setting the external flags. You can use $this->PageNo() to determine whether to include the header and footer or not.
For instance if you'd want to exclude the header and footer on the first page, like I did:
function Footer() {
if($this->PageNo() != 1){
// footer code
}
}
If you'd want to let's say exclude them on several pages and not write an endless if statement you should just put the page numbers to exclude in an array and check with in_array() whether the header and/or footer should be included.
You can define multiple different types of headers and footers by calling functions outside the class:
class PDF extends FPDF {
function Header(){
if(!empty($this->enableheader))
call_user_func($this->enableheader,$this);
}
function Footer(){
if(!empty($this->enablefooter))
call_user_func($this->enablefooter,$this);
}
}
$pdf = new PDF('P');
$pdf->SetTextColor(0);
$pdf->SetFont('Arial','B',10);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Cell(50,6,'Headerless & Footerless page');
$pdf->enableheader = 'header1';
$pdf->AddPage();
$pdf->enablefooter = 'footer1';
$pdf->AddPage();
$pdf->AddPage();
$pdf->enableheader = 'header2';
$pdf->AddPage();
$pdf->enablefooter = 'footer2';
$pdf->Output();
function header1($pdf){
$pdf->Cell(50,6,'Header type 1',1,0,'L');
}
function footer1($pdf){
$pdf->SetY(280);
$pdf->Cell(50,6,'Footer type 1',1,0,'L');
$pdf->Cell(0,6,"Page: {$pdf->PageNo()} of {nb}",1,0,'R');
}
function header2($pdf){
$pdf->Cell(50,6,'Header type 2',1,0,'L');
}
function footer2($pdf){
$pdf->SetY(280);
$pdf->Cell(50,6,'Footer type 2',1,0,'L');
$pdf->Cell(0,6,"Page: {$pdf->PageNo()} of {nb}",1,0,'R');
}
The trick to footers is that the footer is added when the next page is created, with the last footer being added when the output is closed.
Therefore, you have to define the header before the page is added, and the footer afterwards but before the next page.