I’m working with the FPDF library to generate PDFs.
At some point I include an image into the pdf (in the header) and do:
require_once APPPATH . '../vendor/setasign/fpdf/fpdf.php';
class cifpdf extends FPDF {
public function __construct($params = array())
{
$this->CI = &get_instance();
$orientation = array_key_exists('orientation', $params) ? $params['orientation'] : 'P';
$mesure = array_key_exists('mesure', $params) ? $params['mesure'] : 'mm';
$format = array_key_exists('format', $params) ? $params['format'] : 'A5';
parent::__construct($orientation, $mesure, $format);
}
public function Header()
{
$logo = base_url('assets/promoters-landingpage/img/logo-inverted.png');
// Logo
$this->Image($logo, 10, 6, 30);
$this->Rect(10, 17, 129, 0.1);
// Saut de ligne
$this->Ln(10);
}
My issue is coming from the $this->Image($logo, 10, 6, 30); line
Looking at the Image function in the pdf vendor library, I tried to debug and found out the bug was coming from this part:
function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
{
// Some Code
if(!isset($this->images[$file]))
{
// Some Code
$type = strtolower($type);
if($type=='jpeg')
$type = 'jpg';
$mtd = '_parse'.$type;
if(!method_exists($this,$mtd))
$this->Error('Unsupported image type: '.$type);
$info = $this->$mtd($file);
// Some Code
}
// Some Code
}
So the exact line that causes the time out error is $info = $this->$mtd($file); (line 885 in the library).
I;m not sure what $mtd is? It seems defined a few lines above in $mtd = '_parse'.$type; but they do not associate it to $this. Not sure what is this.
I’m using Nginx. That same piece of code was working fine on Apache 2.
Thanks for your support.
Related
<?php
class Mail{
private $image_box;
public $count = 0;
public $zipper;
$encoding = "base64";
$type = "image/png";
public function __construct(){
$this->turnToImage();
}
public function addToObject($object_name, $object_data){
//save to object
// $object_name : $object_data
}
public function turnToImage(){
$this->image_box = $_POST['canvas_data'];
foreach($this->image_box as $img)
{
$this->count++;
$data = substr($img, strpos($img, ","));
$image_name ="OrderImg".$this->count.".png";
$this->addToObject($image_name, $data);
}
}
public function toZip(){
}
}
$Mail = new Mail();
?>
i have canvas fabric image data , and i want to decode to image and zip it up and send by mail.
so 1st question is how do i save the my data to object and be like this ?
object{
$object_name1 : object_data1,
$object_name2 : object_data2
}
2nd how do i zip them up ? after i store to object and some how zip them up together as one zip file ? i am stucked here.
I'm using TCPDF to generate pdf reports. I need custom headers and footers, so I extended the original class to overwrite the Header and Footer methods as suggested in the official documentation (https://tcpdf.org/examples/example_002.phps).
Here you are the code:
class AppPdf extends \TCPDF {
CONST LOGO_PATH = '/../../../public_html/public/images/logo-big.png';
private $xLogo;
private $yLogo;
private $wLogo;
public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false, $xLogo = 8, $yLogo = 0, $wLogo = 50) {
parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
$this->xLogo = $xLogo;
$this->yLogo = $yLogo;
$this->wLogo = $wLogo;
}
public function Header() {
$this->Image(__DIR__ . self::LOGO_PATH, $this->xLogo, $this->yLogo, $this->wLogo);
}
public function Footer() {
$this->SetXY(34,260);
$this->SetFont('Helvetica', 'I', 8);
$this->SetTextColor(0, 0, 0);
$this->MultiCell(130, 20, "footer text", 0, "C", false);
}
}
Then I have a base template that it is used for all the generated documents:
class BasePdf {
CONST CREATOR = 'Creator';
CONST TITLE = 'Title';
CONST PDF_FONT_NAME_MAIN = 'Times';
CONST PDF_FONT_SIZE_MAIN = 11;
protected $pdf;
public function __construct($xLogo = 8, $yLogo = 0, $wLogo = 50)
{
$this->pdf = new AppPdf('P', 'mm', 'A4', true, 'UTF-8', false, false, $xLogo, $yLogo, $wLogo);
$this->pdf->SetCreator(self::CREATOR);
$this->pdf->SetAuthor(self::CREATOR);
$this->pdf->SetTitle(self::TITLE);
$this->pdf->SetFont(self::PDF_FONT_NAME_MAIN, "", self::PDF_FONT_SIZE_MAIN);
}
public function getPdf()
{
return $this->pdf;
}
}
The base template is used as shown in the following class:
use AppBundle\Entity\HPVExam;
class HPVReport extends BasePdf
{
public function __construct(HPVExam $HPVExam)
{
parent::__construct(8, 10, 75);
$this->pdf->AddPage();
}
}
The problem is that this code generates pdfs with an annoying horizontal line in the top and another one in the footer, as you can see in the following image .
I have already tried the suggestions provided here PHP / TCPDF: Template Bug? and here Changing or eliminating Header & Footer in TCPDF but without luck.
What I'm doing wrong? It seems that the original Header and Footer methods are not correctly overwritten... Any idea? Thank you!
Just say to TCPDF that dont print the header or go and modify the source....
$pdf->SetPrintHeader(false);
For a dashboard-app, I want to create a widget-component in my CI3-project. I found a similar question: How to create a widget system in Codeigniter, but it seems that this is either outdated or just not usable for CI3. Even the link with the "more information" results in a 404.
I googled that of course, but didn't find anything useful. One post was from 2013, which showed that example von the CI2 base.
However, maybe this plugin still works and I have an error in my setup.
I placed a file "Widget.php" in
/application/third_party/
with the following content:
class Widget
{
public $module_path;
function run($file) {
$args = func_get_args();
$module = '';
/* is module in filename? */
if (($pos = strrpos($file, '/')) !== FALSE) {
$module = substr($file, 0, $pos);
$file = substr($file, $pos + 1);
}
list($path, $file) = Modules::find($file, $module, 'widgets/');
if ($path === FALSE) {
$path = APPPATH.'widgets/';
}
Modules::load_file($file, $path);
$file = ucfirst($file);
$widget = new $file();
$widget->module_path = $path;
return call_user_func_array(array($widget, 'run'), array_slice($args, 1));
}
function render($view, $data = array()) {
extract($data);
include $this->module_path.'views/'.$view.EXT;
}
function load($object) {
$this->$object = load_class(ucfirst($object));
}
function __get($var) {
global $CI;
return $CI->$var;
}
}
In my application/widgets-folder, I have a file called News.php.
<?php
class News extends Widget
{
function run() {
die('here');
}
}
In application/libraries/ I placed a file widgetlib.php :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class widgetlib {
function __construct() {
include(APPPATH . '/third_party/Widget.php');
}
}
and finally, in my view:
<?php widget::run("News"); ?>
Then I checked my autoloader-class in application/config/autoload.php:
$autoload['libraries'] =
'session',
'database',
'widgetlib'
);
This results in:
A PHP Error was encountered
Severity: Error
Message: Class 'Modules' not found
Filename: third_party/Widget.php
Line Number: 20
and
Severity: Runtime Notice
Message: Non-static method Widget::run() should not be called statically, assuming $this from incompatible context
Filename: views/index.php
I'm stamping my PDF documents with FPDI and TCPDF functions, and I'm trying to figure it out, how can I add a line below the text in Header, and upon the text in footer. Here is my code:
<?php
require_once('lib/tcpdf/tcpdf.php');
require_once('fpdi.php');
$fullPathToFile = "TestClanek6.pdf";
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile; //global
if(is_null($this->_tplIdx)) {
// number of pages
$this->numPages = $this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
if($this->page > 0) {
//$this->SetPrintHeader(true);
$this->SetFont('times', 'I', 11);
$this->SetTextColor(0);
$this->Write(15, "Vol. 1, No. 15, Year: 2015, duff");
$this->Image('logo.png', 100, 2, 75,7);
} //end of IF
$this->useTemplate($this->_tplIdx, 0, 0,200);
} //end of HEADER
function Footer() {
if($this->page > 0) {
$this->SetY(-20);
$this->SetFont('times', 'I', 11);
$this->SetTextColor(0,0,0);
$this->Write(0, "Page", '', 0, 'C');
} //end of if
} // end of footer
} //end of CLASS
// new PDF file
$pdf = new PDF();
$pdf->addPage();
if($pdf->numPages>0) {
for($i=1;$i<=$pdf->numPages;$i++) {
$pdf->endPage();
$pdf->_tplIdx = $pdf->importPage($i);
$pdf->AddPage();
//$pdf->SetPrintHeader(false);
//$pdf->SetPrintFooter(false);
}
}
$file_time = time();
$pdf->Output("$file_time.pdf", "F");//, "I");
echo "Link: '<a href=$file_time.pdf>Stamped article</a>'";
?>
I've tried a lot of things like setPrintHeader(), etc. but nothing what I've found works for me. Could i please somebody to help?
Thank you.
duff
You can use the Line method to draw a line in FPDF. If you want a straight horizontal line, just make sure the ordinates (y values) for the start and end of the line are the same. Something like this for example:
$pdf->Ln(15,$pdf->y,200,$pdf->y);
You would modify the values to suit your needs and insert it in the overridden methods for Header and Footer as appropriate for your application.
Better would be to leave the two methods (Header and Footer) empty. this way you would overwrite the drawing from super-class.
like this:
class EmptyFPDI extends FPDI
{
public function Header()
{
}
public function Footer()
{
}
}
I am using FPDF to create a report from a Mysql database. The PDF is created fine but I would like to display the headers for each column at the top of every page for easier navigation. Does anyone have any idea on how to do this?
Thanks for the help.
try to extend fpdf class, and implement your Ln function: every time you add a line, decrement counter of line number, and when is 0, add new page with header.
class _PDF extends FPDF
{
var $_num_Rows = null;
var $_heightCell = null;
const NUM_ROWS = 48; // set number line in page
function _PDF($orientation = 'P',$unit = 'mm' ,$page = 'A4')
{
$this->_numRows = self::NUM_ROWS;
$this->_heightCell = 4;
$this->SetAutoPageBreak(true,10);
$this->FPDF($orientation, $unit, $page);
}
public function init_numRows()
{
$this->_numRows = self::NUM_ROWS;
}
public function dec_numRows()
{
$this->_numRows--;
}
function Ln()
{
if ( $this->_numRows )
{
parent::Ln();
$this->dec_numRows();
}
else
{
$this->addHeader();
}
}
function addHeader()
{
$this->init_numRows();
$this->AddPage();
// ...
}
}
$pdf = new _PDF();