How to Print Title on Every Page generated By FPDF - php

i use FPDF to generate Report From Database and i want the title of the Report to be Printed on the header of the PDF generated
my code look like this
function Header()
{
$name="Export PDF";
$this->SetFont('Arial','B',15);
$this->Image('images/pdflogo.png', 5,5,60);
$this->Image('images/hr1.jpg', 10,25,190);
$this->Text(100,25,'$d');
$this->Cell(80);
$this->SetFont('Arial','B',9);
$this->Ln(20);
}
?>
can any body show me how to do this Please !!

Use: $this->Cell(80, 10, 'Example Report Title' );
It will show Example Report Title on every pages header.

Related

Hook to replace a page's content with a string contained in a variable in Wordpress

I'm building a wordpress plugin where I have to render some specific content when appropriated instead of pages.
Here is the current situation (all files are in the root folder of the plugin):
my-template.php
<?php
echo "hi world";
my-plugins-main-php-file.php
//...logic to determine if the content of the page has to be rendered...
function load_template(){
return __DIR__."/my-template.php";
}
add_action('template_include', 'load_template');
And here is the desired result:
my-plugins-main-php-file.php
//...logic to determine if the content of the page has to be rendered...
add_action('string_include_or_some_other_hook',"hi world");
Is there a hook or some other method I can use to render some wordpress content directly from a string?
Thanks!
Here is the solution to my question, thanks to #ArtisticPhoenix:
my-plugins-main-php-file.php
//...logic to determine if the content of the page has to be rendered...
function load_template(){
return __DIR__."/my-template.php";
}
add_action('template_include', 'load_template');
$string = "hi world!";
add_filter('the_content', function() use ($string){
return $string;
});
my-template.php
<?php
the_content();

Content Replacement not in hyperlinks

I have a simple function that all content on my website is filtered through to capitilize the company's name. Here is the function:
function brand_capitilize($content) {
$content = str_replace("Brand", "BRAND", $content);
return $content;
}
I need to modify this to ignore the word "Brand" if it's in a url. For example, I do not want the word 'Brand' to be capitalized in the following situation:
View PDF
If I feed this through brand_capitilize() right now, I end up with:
View PDF

how to write a script output in a post in the act of creation of these posts?

Hello community WordPress forum.
I have a need to write the posts the output of a function, but all to getting to do is to display real-time and run this script / function in all posts, with I do to write the posts the output of a function instead of display it in posts?
because what 's happening is that the script is running on all posts, and each refresh / access the pages, a new script number is generated! I would like the generator create a different number for each post, but write to output them, and not display a number to each new access.
// Declare the function
function gen_num()
{
// DETERMINE THE CHARACTER THAT CONTAIN THE PASSWORD
$caracteres = "012345678910111213141516171819";
// Shuffles THE CHARACTER AND HANDLE ONLY THE FIRST 10
$mistura = substr(str_shuffle($caracteres),0,10);
// DISPLAYS THE OUTCOME
print $mistura;
}
// Add custom post content, inject function in content.
function add_post_content($content) {
gen_num();
return $content;
}
add_filter('the_content', 'add_post_content');
see in herculestest.tk, browse the pages, make f5 to refresh.
Thank you very much.
==========================================
another attempt:
I created a custom field from ACF plugin named: numeration:
function gen_num()
{
global $post;
$mistura = get_post_meta( $post->ID, 'numeration', true );
if ( '' == $mistura ) {
//DETERMINE THE CHARACTER THAT CONTAIN THE PASSWORD
$caracteres = "012345678910111213141516171819";
// Shuffles THE CHARACTER AND HANDLE ONLY THE FIRST 10
$mistura = substr(str_shuffle($caracteres),0,10);
update_post_meta( $post->ID, 'numeration', $mistura );
}
//DISPLAYS THE OUTCOME
print $mistura;
}
// Add custom post content, inject function in content.
function add_post_content($content) {
gen_num();
return $content;
}
add_filter('the_content', 'add_post_content');
this solution to not change the number each new access, now writing these data permanently in the database ?? because're not recording! if I change my theme, all the numbers in all the posts disappear, and if I make any errors in functions, php, these add up numbers, because they depend on the function running at him display the values, and worst of all, to fix the functions.php, the script will return to run and therefore, will be a re-run, which means that it will generate new numbers on all posts !! and this can not happen, should I ever have the same values!
The Post ID is unique in WordPress, is that number adequate for your needs?
Otherwise you'd need to generate a unique ID when the post is published and save it in a custom field. (Instead of your gener_numb function look into uniqid: http://php.net/manual/en/function.uniqid.php.)

fpdf multiple calls to fpdf class

I have a list of customers who need to get a dynamically generated pdf.
class PDF extends FPDF
{
// Page header
function Header()
{
global $backToTOC;
// Logo
$this->Image('logo.jpg',70,10);
$this->Write(5,'Back to TOC',$backToTOC);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(5,'Contact support: 1-800-support');
$this->Cell(5,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
foreach ($customer as $k => $v)
{
$pdf = new PDF();
$pdf->AliasNbPages();
//....pdf stuff.....
$pdf->Output($v.'.pdf','F');
}
the result of this is a divide by zero error.
PHP Warning: Division by zero in /var/www/lib/fpdf/fpdf.php on line 796
and the footer page numbers show 0. any thoughts?
The Cell method expects the second parameter to be the cell height, not the cell content (see Cell method). When calling:
$this->Cell(5,'Page '.$this->PageNo().'/{nb}',0,0,'C');
you are using 'Page '.$this->PageNo().'/{nb}' as the height and 0 as the content. It should be:
$this->Cell(5, $cellHeight, 'Page '.$this->PageNo().'/{nb}',0,0,'C');
Try setting your font right after creating the PDF object, it worked on my machine.
Also if it doesn't work, please paste a stacktrace.

PHP: passing array elements into class function in fpdf

Working on a project and need to generate pdf of the product details using FPDF. The product details are passed into an array and I need the following to get each of the variable elements in the array '$prod_details' into the functions within the class 'PDF' as shown below:
Examples of how I tried passing the variable array elements:
$this->Cell(30,8,$prod_data['prod_name'],0,0,'C');
$this->Cell(30,10,$prod_data['company_name']);
$this->Cell(20,8,$prod_data['prod_cost'],0,0,'C');
I have tried running this script but I keep getting an error message 'Cannot access empty property'...
find the codes below
<?php
#include_once("includes/db.php");
require('fpdf/fpdf.php');
#include_once("includes/class_product_info.php");
$obj = new allProducts();
$prod_data = array();
if(isset($_GET['c_id'])){
$prod_data = $obj->getProdDetails($_GET['c_id']);
class PDF extends FPDF
{
public $prod_data;
public function createData($input){
$this->prod_data = $input;
}
function Header()
{
// Logo
$this->Image('big_logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',20);
// Move to the right
$this->Cell(40);
// Title
$this->Cell(30,10,$this->prod_data['company_name']);
// Draw an header line
$this->Line(10,26,200,26);
// Line break
$this->Ln(20);
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Begin with regular font
$this->SetFont('Arial','',9);
// Then put a blue underlined link
//$this->SetTextColor(0,0,255);
$this->SetFont('','U');
$this->Write(10,$this->prod_data['company_name'],'http://www.skills.com');
// Arial italic 8
$this->SetFont('Arial','I',9);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().' ',0,0,'R');
}
function prodDetailTop()
{
// Course title cell
$this->Cell('',10,'',0,0,'C');
$this->Ln();
/* Build cells for the first row */
$this->SetFont('Arial','',10);
$this->SetY(40);
// First Row
$this->Cell(35,8,'Product Name : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['prod_name'],0,0,'C');
$this->SetX(150);
$this->Cell(25,8,'Product Cost : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['prod_cost'],0,0,'C');
$this->Ln();
// Second Row
$this->Cell(35,8,'Discount : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['disc_amt'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'No Purchased : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['items_count'].' product(s)',0,0,'L');
$this->Ln();
}
function prodDetailBtm()
{
$this->SetY(80);
$this->Write(10,$this->prod_data['prod_desc']);
}
function generatePageData()
{
$this->AddPage();
$this->prodDetailTop();
$this->prodDetailBtm();
}
}
$pdf = new PDF();
$pdf->createData($prod_data);
//$pdf->Header();
$pdf->generatePageData();
$pdf->Output();
}
else {
?>
<script language="javascript">
window.location = "prod_invoice_err.php";
</script>
<?php
}
?>
Hope to get some help.
Your question is a little vague. It would be helpful it you asked specifically what you're trying to accomplish.
But the first thing I see is that your subclass of the fpdf class, you don't need to write functions to do each and everything you want to do with the pdf. You only need to extend the parts of the class you are overriding (or extending), like header and footer.
So extend it, manipulate header and footer, then close the class. Create your $pdf instance of your new fpdf class, then manipulate that object with your data. You don't need to 'pass in' that data at all.
for instance:
$pdf->Ln(10);
$pdf->Cell($w,9,$title,1,1,'C',true); //from fpdf tutorial
Or, if that doesn't accomplish what you want (although I can't see why it wouldn't, I've done this lots of times), you can always override the constructor. Pass in an array (or event a custom object that you create), and store that in a private variable.

Categories