PHP - DOMPDF 1 function works, another does not - php

I am working with DOMPDF currently, and I trying to work out why my PDF's are not being created correctly.
When I run the following code,
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
$dompdf->output_html();
//$dompdf->load_html($html);
//$dompdf->set_paper("a4", "portrait" );
//$dompdf->render();
//$dompdf->stream($filename . ".pdf");
}
?>
In my browser I see the PDF content that I expect to be generated, however if I run this code,
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
//$dompdf->output_html();
$dompdf->load_html($html);
$dompdf->set_paper("a4", "portrait" );
$dompdf->render();
$dompdf->stream($filename . ".pdf");
}
?>
I get no repsonse, where I would be expecting to get a download window. Is there a reason for this, I wonder if poorly formatted HTML could the problem?

Your CI plugin code should work on 1.7.x. Here's mind for your reference:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=true) {
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename);
} else {
$CI =& get_instance();
$CI->load->helper('file');
write_file($filename, $dompdf->output());
}
}

Related

Fatal error: Class 'DOMDocument' not found in Codeigniter 2 and PHP 5.1

I use PHP 5.6 in my localhost and this script for generating pdf is working well. But in the server which is using PHP 5.1 I got that error. Here is the script in application/libraries/pdf.php.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once (dirname(__FILE__)) . '/dompdf/dompdf_config.inc.php';
class Pdfgenerator {
function __construct() {
$this->ci =& get_instance();
$this->dompdf = new DOMPDF();
}
function generate($data){
$html = $this->ci->load->view($data['template'],$data,true);
$paper_size = isset($data['paper_size']) ? $data['paper_size'] : 'A4';
$orientation = isset($data['orientation']) ? $data['orientation'] : 'potrait';
$this->dompdf->set_paper($paper_size,$orientation);
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream($data['filename'].'.pdf',array('Attachment'=>0));
}
}
What should I change to make it works in PHP 5.1?

DOMPDF Doesn't Work if HTML is Already Present

I have a function that uses DOMPDF to generate a .pdf of whatever I set for the $html argument.
function generate_pdf($html){
//DOMPDF stuff
}
This works, but the problem I'm running into is that when I call this function from a page that already has HTML content, it fails.
Fails...
<?php
require_once '../header.php'; //This has HTML content in it.
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
This also fails...
<?php
echo 'stuff';
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
Works...
<?php
$html = '<h1>stuff</h1>';
generate_pdf($html);
?>
Is there any way around this?
Contents of function generate_pdf($html)
function generate_pdf($html){
//Get the necessary dompdf files
include_once DOMPDF_PATH . '/autoload.inc.php';
// instantiate and use the dompdf class
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml($html);
// Render the HTML as PDF
$dompdf->render();
//Output the PDF
$dompdf->stream();
}
Note that the file in which this function lives has a namespace, so that's why $dompdf = new \Dompdf\Dompdf(); might appear wrong, but that line is working fine.
CHANGED ANSWER after full code was added to question:
I have the complete HTML content in an included file, no extra HTML before or after it (also no php echoing).
I made a mistake before - I mixed up the inclusion of the dompdf autoload and the content - but see below how it works in my case. This is the content of a php file, there is no function:
<?php
require_once '../../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->set_option('isPhpEnabled', true);
ob_start();
//Here I am getting a few variables (from the URL via GET) which I use in the included php file
include_once "your_content_in_one_file.php";
$html = ob_get_contents();
ob_end_clean();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($pdf_name);
?>
It ended up being related to the fact that ob_start(); had already been called somewhere else in the system. Using...
if (ob_get_level()){
ob_end_clean();
}
fixed my issue. Hope it helps someone else.

How to upload file pdf from dompdf to database in codeigniter?

How can I upload file from dompdf to database?
If I have controller like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sertifikat extends CI_Controller {
public function Sertifikat_pdf(){
$this->load->library('session');
$this->load->model('model_users');
$data['userData'] = $this->model_users->fetchUserData($this->session->userdata('user_id'));
$this->load->library('pdf');
ini_set('max_execution_time', 300);
$this->pdf->setPaper('A4', 'landscape');
$this->pdf->filename = "Sertifikat_magang.pdf";
$this->pdf->load_view('view_sertifikat', $data);
}
}
In that controller I just show file of pdf, but how can I upload the file of pdf to database?
This might do the trick
$dompdf->render();
$pdf = $dompdf->output();
$file_location = $_SERVER['DOCUMENT_ROOT']."App_folder_name/reports/".$pdf_name.".pdf";
file_put_contents($file_location,$pdf)
where reports is folder name where all PDF will be saved as output.

how to use utf-8 in mPDF library - codeigniter

I want to use [mpdf][1] library in codeigniter framework .
now,I want to use utf-8 for persian language .
$this->load->library('pdf');
$pdf = $this->pdf->load();
$html = iconv("utf-8","UTF-8//IGNORE",$html);
$pdf->SetDirectionality('rtl');
$pdf->mirrorMargins = true;
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer for good measure <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="😉" draggable="false" class="emoji">
$pdf->WriteHTML( $html); // write the HTML into the PDF
$pdf->Output($pdfFilePath, 'F'); // save to file because we can
the result of pdf file :
//libraries/pdf.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class pdf {
function pdf()
{
$CI = & get_instance();
log_message('Debug', 'mPDF class is loaded.');
}
function load($param=NULL)
{
include_once APPPATH.'/third_party/mpdf/mpdf.php';
if ($params == NULL)
{
$param = '"fa","A4","","",10,10,10,10,6,3';
}
return new mPDF($param);
}
}

Error in DOM PDF using Codeigniter

I have downloaded dompdf libraries from github https://github.com/EllisLab/CodeIgniter/wiki/PDF-generation-using-dompdf
Controller function
function pdf()
{
$this->load->helper('dompdf');
// page info here, db calls, etc.
$this->data['query'] = $this->my_model->dashboard_db();
$this->data['queryCmt'] = $this->my_model->dashboard_comment_task_db();
$html = $this->load->view('home',$this->data, true);
$fn = 'file_pdf';
$data = pdf_create($html, $fn, true);
write_file($fn, $data);
//if you want to write it to disk and/or send it as an attachment
}
Dom pdf Helper function
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename.".pdf");
} else {
return $dompdf->output();
}
}
?>
This gives me a server error, but if i remove a pdf_create() in controller in generate one pdf file. Can anyone help one this
In your home.php (view) file,
Remove <thead> and <tbody> tags and remove space between <html><head> , </head><body> and </body></html>
It will works fine.!

Categories