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.
Related
Can someone help me? I'm using dompdf in codeigniter, i was trying all solution and then no one can resolv my problem. When using localhost there was no problem, after hosting there was an error Unable to stream pdf: headers already sent please help me i'm still leraning.
Here's my controller Laporanpdf.php
`<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Laporanpdf extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model_laporan');
$this->load->library('dompdf_gen');
}
public function index(){
$data['title'] = 'Cetak Laporan Kinerja'; //judul title
if($this->session->userdata('level')=='1-Admin'){
$data['pengguna'] = $this->model_laporan->pengguna_admin(); //query model
$data['kinerja'] = $this->model_laporan->kinerja_admin(); //query model
$data['awal'] = strtotime($this->input->get('awal'));
$data['jbtn_atasn'] = $this->input->get('jbtn_atasn');
$data['nip_atsn'] = $this->input->get('nipeg');
$data['pangkat_atsn'] = $this->input->get('pngkat');
$data['gol_atsn'] = $this->input->get('gol');
}else{
$data['pengguna'] = $this->model_laporan->pengguna_else(); //query model
$data['kinerja'] = $this->model_laporan->kinerja_else(); //query model
$data['jbtn_atasn'] = $this->input->get('jbtn_atasn');
$data['nip_atsn'] = $this->input->get('nipeg');
$data['pangkat_atsn'] = $this->input->get('pngkat');
$data['gol_atsn'] = $this->input->get('gol');
}
if($this->session->userdata('level')=='1-Admin'){
if ($this->model_laporan->kinerja_admin()==null){
//pesan error
echo "<script>alert('Kinerja dengan tanggal tersebut tidak tersedia. Silahkan masukan tanggal yang sesuai.');
window.location='/kinerja/index.php/kinerja';
</script>";
exit();
} else {
$this->load->view('v_cetak', $data, TRUE);
}
}else{
if ($this->model_laporan->kinerja_else()==null){
//pesan error
echo "<script>alert('Kinerja dengan tanggal tersebut tidak tersedia. Silahkan masukan tanggal yang sesuai.');
window.location='/kinerja/index.php/kinerja';
</script>";
exit();
} else {
$this->load->view('v_cetak', $data, TRUE);
}
}
$paper_size = 'A4'; //paper size
$orientation = 'potrait'; //tipe format kertas
$html = $this->output->get_output();
$this->dompdf->set_paper($paper_size, $orientation);
//Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("laporan.pdf", array('Attachment'=>0)); //preview pada browser
}
}`
Here's my Dompdf_gen.php I put in application/library
`<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dompdf_gen {
public function __construct() {
require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php'; // #version 0.5.1.htischer.20090507
$pdf = new DOMPDF();
$CI =& get_instance();
$CI->dompdf = $pdf;
}
}`
Please check in your error logs. If any warnings or other output are generated before dompdf tries to render the output then it will fail. Address any errors you find in the logs and try again.
Other likely causes might be an empty line after a closing '?>' tag in one of your other classes. Avoid using the closing php tags in any of your models/controllers/libraries as they are unnecessary and can cause issues like this.
If you are not able to find the issue that is causing this, as a last ditch attempt you can try using output buffering to stop any leaks (See: http://php.net/manual/en/book.outcontrol.php).
I'm having some little trouble getting the Codeigniter mPDF library to work. Below is the class in 'application/libraries':
class mpdf {
function mpdf() {
$CI = & get_instance();
log_message('Debug', 'mPDF class is loaded.');
}
function load($param = NULL) {
include_once APPPATH . 'third_party/m_pdf/mpdf.php';
if ($params == NULL) {
$param = '"en-GB-x","A4","","",10,10,10,10,6,3';
}
return new mPDF($param);
}
}
while my function that is supposed to print out the pdf is as below:
//pdf
public function outputPDF() {
//this data will be passed on to the view
$data['the_content'] = 'mPDF and CodeIgniter are cool!';
//load the view, pass the variable and do not show it but "save" the output into $html variable
$html = $this->load->view('pdf_output', $data, true);
//this the the PDF filename that user will get to download
$pdfFilePath = "the_pdf_output.pdf";
//load mPDF library
$this->load->library('mpdf');
//actually, you can pass mPDF parameter on this load() function
$pdf = $this->mpdf->load();
//generate the PDF!
$pdf->WriteHTML($html);
//offer it to user via browser download! (The PDF won't be saved on your server HDD)
$pdf->Output($pdfFilePath, "D");
}
Below is the error:
Fatal error: require_once(): Failed opening required
'X:/xampp/htdocs/.../application/third_party/m_pdf/config_cp.php'
(include_path='.;X:\xampp\php\PEAR') in
X:\xampp\htdocs...\application\third_party\m_pdf\mpdf.php on line 39
Kindly assist
I had the same problem with the library. Now I got it working on my CodeIgniter application.
In order to make it work, I had to put the library folder in the third_party folder (inside application folder). Then I made an autoloader. Inside libraries folder I created this file called 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 = '"en-GB-x","A4","","",10,10,10,10,6,3';
}
return new mPDF($param);
}
}
Then, you can load the library as CodeIgniter expects:
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer
$pdf->WriteHTML($html); // write the HTML into the PDF
$pdf->Output($pdfFilePath, 'F');
$html is the string containing the html view you want to export.
$pdfFilePath is the string path where we are saving our pdf.
I've been playing around trying to generate QR Codes from a string without any luck. I'm using CodeIgniter. I've tried 2 different packages from Packagist, bacon/bacon-qr-code, and endroid/qrcode. Below is the code in my controller for Bacon :
$renderer = new \BaconQrCode\Renderer\Image\Png();
$renderer->setHeight(256);
$renderer->setWidth(256);
$writer = new \BaconQrCode\Writer($renderer);
$writer->writeFile('Hello World!', 'qrcode.png');
When I run this code I get the error 'The phpass class file was not found'.
So I then installed phpass through spark, and I still get the same error. Can anyone tell me what I'm doing wrong?
First one is working as well (probably second one too).
You need to use it this way (at least):
APPPATH . 'libraries/Qrcode.php'
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
use \BaconQrCode\Renderer\Image\Png;
use \BaconQrCode\Writer;
class Qrcode
{
public function test()
{
$renderer = new Png();
$renderer->setHeight(256);
$renderer->setWidth(256);
$writer = new Writer($renderer);
$writer->writeFile('Hello World!', 'qrcode.png');
//var_dump($writer);
}
}
APPPATH . 'controllers/Test.php'
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->qrcode();
}
public function qrcode()
{
$this->load->library('qrcode');
$this->qrcode->test();
}
}
And image will be generated in FCPATH . 'qrcode.png' file.
you can generate QR Codes of string using Google QR Codes API.
https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=Hello+world&choe=UTF-8
300x300 is your image size.
chl - a url encoded string to convert it into qr code.
I want to ask how can I convert excel to pdf?
I want to put 5 excel files and after I click upload in convert to 1 pdf?
for now I use in this code:
/**
* Example: DOMPDF
*
* Documentation:
* http://code.google.com/p/dompdf/wiki/Usage
*
*/
public function index() {
// Load all views as normal
$this->load->view('test');
// Get output html
$html = $this->output->get_output();
// Load library
$this->load->library('dompdf_gen');
// Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("welcome.pdf");
}
the library:
class Dompdf_gen {
public function __construct() {
require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php';
$pdf = new DOMPDF();
$CI =& get_instance();
$CI->dompdf = $pdf;
}
}
first of things , you have to dump all excel to php array or var , after that you can create pdf as you want , this is simple php script for convert excel to pdf , you can use it and convert it to subclass of codeigniter as well
please check this url
PHP read excel file
when you use that class your excel data will be with you so you can use another class for pdf and this class will help you
codeigniter create PDF file
hopeful this is helpful for you .
I do not understand the problem.
like in this code:
$this->load->helper(array('dompdf', 'file'));
// page info here, db calls, etc.
$html = $this->load->view('home', $data, true);
pdf_create($html, 'filename')
or
$data = pdf_create($html, '', false);
write_file('name', $data);
how i put after i upload excel file and cover it to pdf?
this function is only for HTML
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.!