Using dompdf i generate pdf file. I am successfully generate it .
But after generating it when I open it in Foxit reader it's open very small size in 51.79% mode. When i select 'Fit width' or 125% it show properly.
When I open the pdf by default it open 125% mode can it be possible ?
I used following function:
function pdf_create($html, $filename)
{
ini_set("memory_limit", "50M");
//define pdf store path
$invoice_pdf_path = ABSOLUTE_PATH;
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper("a4", "portrait");
$dompdf->render();
//$dompdf->stream("abc_out.pdf");
$pdf = $dompdf->output();
#file_put_contents($invoice_pdf_path . $filename . ".pdf", $pdf);
}
With dompdf 0.6 beta you can set the default view of the PDF by adding a meta tag in your HTML source code:
<meta name="dompdf.view" content="FitH" />
Possible values are "XYZ", "Fit", "FitH", "FitV", "FitR", "FitB", "FitBH", "FitBV", described here
Related
Using Dompdf to generate a pdf file. The file is created successfully and downloads but the page I'm running it on generates a fatal error - Class 'Dompdf\Dompdf' not found.
I've downloaded the latest package and uploaded it to the server.
I'm using the quick start code from here - https://github.com/dompdf/dompdf#easy-installation
require_once 'path/to/the/folder/here/dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
I've read all the related support posts I can find but nothing seems to be working
Try this one
<?php
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>
This works for me. Just instantiate the dompdf class as follow: $dompdf = new \Dompdf\Dompdf();
<?php
require_once 'path/to/the/folder/here/dompdf/autoload.inc.php';
// Instantiate and use the dompdf class, as follow:
$dompdf = new \Dompdf\Dompdf();
// Then continue your code here...
Actually, generate the dynamic PDF successfully with the help of dompdf library in CI but not send the pdf dynamic those generated here. So tell me how to do this?
$filename = "newpdffile";
require_once APPPATH.'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml($output);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream($filename);
$file_to_save="/application/dompdf/";
$pdf=file_put_contents($file_to_save, $dompdf->output());
$this->email->from('support#aurorax.co', 'aurora exchange');
$this->email->to('masnad#aurorax.co');
$this->email->subject('pdf');
$this->email->attach($pdf);
$this->email->message('Hello!');
If you have sucesssfully generated PDF and saved in dompfdf directory
then
$pdf= 'http://yourwebsite.com/dompfdf/<?php echo $pdfNameYouGenerated; ?>';
$this->email->attach($pdf);
Use APPPATH to get the file path
$file_to_save = APPPATH. "application/dompdf/";
$pdf = file_put_contents($file_to_save, $dompdf->output());
When PDF file successfully generated and save in the directory then attach while sending email
$this->email->from('support#aurorax.co', 'aurora exchange');
$this->email->to('masnad#aurorax.co');
$this->email->subject('pdf');
$this->email->attach($pdf);
$this->email->message('Hello!');
Please check below code:
$filename = "newpdffile";
require_once APPPATH.'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml($output);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$output = $dompdf->output();
$filePath = FCPATH.'application/dompdf/filename.pdf';
file_put_contents($filePath, $output);
$this->email->from('support#aurorax.co', 'aurora exchange');
$this->email->to('masnad#aurorax.co');
$this->email->subject('pdf');
$this->email->attach($filePath);
$this->email->message('Hello!');
I am using barryvdh/laravel-snappy to generate pdf file. I have two image files 1. yourlogohere.png is in public/image/ folder and 2. logo2.png is in folder other than public i.e. storage/app/logo and to get this file I defined a route (www.example.org/logo/logo.png) and use following code to access it.
public function logo($filename)
{
$file = Storage::disk('local_logo')->get($filename);
$mime = 'image/png';
return (new Response($file, 200))->header('Content-Type', $mime);
}
Problem:
When I use following code to generate pdf from the html containing the first file, pdf contains the yourlogohere.png image
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/images/yourlogohere.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
But when I do exact same thing for the second file, pdf does not render the image.(When I open the link http://www.example.org/logo/logo2.png in browser I get the image). What am I missing?
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/logo/logo2.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
Thanks,
K
You can also do:
<img src="data:image/jpeg;base64,
{{ base64_encode(#file_get_contents(url('your.image.url'))) }}">
I think I got the what the problem is, the route to access the image is via auth, even when user is logged in while accessing the snappy, the wkhtmltopdf exe runs in a shell that is totally different session. Now the right fix would be to be embed the image in the html that is sent to snappy instead of the link, Which I am not sure how I will do? Any suggestions welcome there.
Update:
I as able to convert the image to data:image/png;base64, and embed it in html.
$html = view('mytemplate.default', compact('variable1', 'variable2'))->render();
/*Convert logo image to base64 before pdf conversion*/
//search for <img src="http://example.org/mytemplate/logo/logo1.png">" and replace the src with data:image/png;base64,
$search = '/(<img\s+src=["\'])([^"\']+)(\/mytemplate\/logo\/)(.*)(\.)(.*?)(["\']\s+[^>]+>)/';
$html = preg_replace_callback($search, function ($matches) use ($invoicedetail) {
$filename = $matches[4] . $matches[5] . $matches[6];
$file = Storage::disk('local_logo')->get('yourlogohere.png');
$mime = "image/png";
$mytemplate = MyTemplate::where('logo_filename', '=', $filename)->first();
if (!empty($mytemplate)) {
$file = Storage::disk('local_logo')->get($mytemplate->logo_filename);
$mime = $mytemplate->logo_mime;
}
$base64 = 'data:' . $mime . ';base64,' . base64_encode($file);
return $matches[1] . $base64 . $matches[7];
}, $html);
$pdf_filename = 'template' . $mytemlpate->id . '.pdf';
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $filename);
$snappy = App::make('snappy.pdf');
I have the following code in my save.php:
<?php
require_once("dompdf/dompdf_config.inc.php");
$LgPath=$_POST['image'];
header("Content-Transfer-Encoding: binary");
header("Content-Type: image/png");
//header("Content-Disposition: attachment; filename=image.png");
$html = file_get_contents($LgPath);
//$html = readfile($LgPath); --only shows the image
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
I take succesfully using HTML2CANVAS the print screen of some element, and then i pass it to the save.php page using $POST. I want that using DOMPDF to put that image in the sample.pdf. Using this code I only have this text in the sample.pdf "‰PNG", and I have don't figure how to fix it. Using readfile i see the image, but that's it... Any suggestions? Thank you.
You're reading your image into a variable and passing it to dompdf. As such dompdf is treating that text that makes up that image as HTML source. Since your image is readable from the PHP script at the location stored in $LgPath you should be able to do something like this:
<?php
require_once("dompdf/dompdf_config.inc.php");
$LgPath=$_POST['image'];
$html = '<img src="' . $LgPath . '">';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
I am trying to save a pdf to disk.
I am using Dompdf and Codeigniter as below:
$this->load->helper('dompdf','file');
$this->load->helper('file');
$saveTo = './labels';
$data['id'] = $this -> uri -> segment(3);
$data['no_of_prints'] = $this -> uri -> segment(4);
$labelname = "Label". $data['id'] ."-". $data['no_of_prints'] . ".pdf";
$html = $this->load->view('label_view', $data, TRUE);
file_put_contents($saveTo."/".$labelname, pdf_create($html, $labelname));
While the labelname gets generated correctly. The file saved to disk reads 0 bytes and as expected I get an Adobe Reader cannot open this file ... error.
I am also using fancybox 2.0 to popup the pdf and when I right click on the popup,(the pdf does not show, i'm guessing pdf plugin issues) I am able to Save as a readable pdf that is not 0 bytes.
It is important for the application to save pdf to disk automatically. How do i save a readable pdf to disk?
I have found some old CI code (pre version 2) that used DomPDF, I had an extra parameter to write as a file.
Is this much different from yours?
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=FALSE, $orientation='portrait')
{
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper( array(0,0, 2.175 * 72, 3.375 * 72), "landscape" ); // credit card size
$dompdf->load_html($html);
$dompdf->render();
if ($stream)
{
$dompdf->stream($filename.".pdf");
}
else
{
$CI =& get_instance();
$CI->load->helper('file');
write_file("./uploads/$filename.pdf", $dompdf->output());
}
}
?>