I'm using the dompdf library to create PDFs from HTML.
I am having a problem rendering images, as they seem to be appearing as red crosses.
$dompdf = new DOMPDF();
$html = '<div><img src="http://www.domain.com/uploads/images/image1.jpg" /><div>';
$dompdf->load_html($html);
$dompdf->set_paper("A4");
$dompdf->render();
$output = $dompdf->output();
$filename = date('dmYHis') . '-lowres.pdf';
file_put_contents('/uploads/pdfs/low-res/' . $filename, $output);
This creates the PDF, but it doesn't seem to find the image.
I've tried to have just <img src="/uploads/images/imag1.jpg" />, but this also renders the image as a red cross.
I'm using the 0.6.0beta3 version of DomPDF
Thanks
Ok, so it seemed having a path such as the full URL doesn't work.
Neither does <img src="/uploads/images..." />`
I changed the path to be <img src="uploads/images..." /> which seemed to work
Related
My s3 url is https://s3-us-west-2.amazonaws.com/bucket_name/test.png
its working fine on browser and showing image not found after including it on pdf file.
$pdf = new \DOMPDFModule\View\Model\PdfModel();
$pdf->setOption("filename", $filename);
$pdf->setOption("paperSize", "a4");
$pdf->setOption("paperOrientation", "landscape");
$dompdf = new \DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdfCode = $dompdf->output();
file_put_contents('path_to_pdf/test.pdf', $pdfCode);
If you use Laravel, you need to add this to your blade.
#php
use Illuminate\Support\Facades\Storage;
$content = Storage::get('vinylmaster.png');
$imageData = base64_encode($content);
$src = 'data:image/png;base64,' . $imageData;
#endphp
<img src="{{$src}}">
if you are not on Laravel, try Embedding images in a PDF file with PHP and DOMPDF
This code works but the resulting file is being opened like HTML for me not as a pdf or download pdf. How can I fix that?
<?php
require_once("dompdf/dompdf_config.inc.php");
//var_dump("dompdf/dompdf_config.inc.php");
$tiket = $_GET["tiket"];
$file = $this->load->view('admin/report/fm-it-01',$tiket);
$dompdf = new DOMPDF();
//var_dump($dompdf->load_html($file));
$dompdf->load_html_file($file);
$dompdf->render();
$dompdf->stream("sample.pdf"); ?>
Try $dompdf->load_html($file); instead of $dompdf->load_html_file($file);
I'm making a download of PDF with DOMPDF. Everything is working fine. The PDF is downloading and opens well in Adobe Reader. But at closing the window, Adobe Reader is asking me if I want to save the PDF file where I didn't change anything in. What is the possible problem that cause this problem?
$filename = $naam.' Factsheet.pdf';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
file_put_contents('pdf/'.$filename, $dompdf->output( array("compress" => 0) ));
if ( !headers_sent() ) {
$dompdf->stream($filename, $options);
}
Can please anybody help me with this problem?
Sorry for grave digging, but i had the same issue, however i fixed my code but i dont know how, here is the working code
$filename = $sm_number . "_" . date('Ymd') .'_OK_CALCOOLING.pdf';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml($content);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$array = array(
"compress" => 0,
"Attachment" => 0
);
$dompdf->stream($filename, $array);
Make sure you add ob_get_clean() when declaring your $html variable.
$html = ob_get_clean(); //ADD THIS LINE WHEN DECLARING HTML VARIABLE
$html .= 'My content here';
$dompdf = new Dompdf();
$dompdf->getOptions()->setIsFontSubsettingEnabled(true);
$dompdf->loadHtml(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("filename.pdf", array("compress" => 0, "Attachment" => false));
exit;
I am basically all the way there. I am unsure how to push the temporary pdf object to imagick to generate and return the jpeg image. Using mPDF's standard output(), I get the pdf rendered to the screen:
$mpdf=new mPDF();
$mpdf->WriteHTML($output);
$img = new Imagick($mpdf->output());
$img->setResolution(300,300);
$img->resampleImage(150,150,imagick::FILTER_UNDEFINED,1);
$img->resizeImage(512,700,Imagick::FILTER_LANCZOS,0);
$img->setImageFormat('jpeg');
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$img->writeImage ("test.jpeg");
Under this scenario I get the pdf outputed to the screen instead of a jpg image returned/created. What is the proper way to pass the pdf into imagick?
I think you need: $mpdf->Output('temp/'.$filename.'.pdf','F');
$mpdf->Output('temp/'.$filename.'.pdf','F');
$pdf_file = 'temp/'.$filename.'.pdf';
$savepath = 'temp/'.$filename.'.jpg';
$img = new imagick($pdf_file);
$img->setImageFormat('jpg');
$img->writeImage($savepath);
echo "<img src='temp/$filename.jpg' />";
http://phpform.net/pdf-to-image.php
I have php code which create pdf thumbnail as follows;
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
?>
Which is working well. But if I want to display the image in a web page, I have to use <img src=""> tag. Is there any way to remove header("Content-Type: image/jpeg");
from the syntax and echo image using <img src="">..? Or anybody tell me how to use the syntax to display the image inside a web page.
I am running apache with php5 in my Windows Vista PC..
With Imagick, you could use base64 encoding:
echo '<img src="data:image/jpg;base64,'.base64_encode($img->getImageBlob()).'" alt="" />';`
However, this method is kind a slow and therefore I recommend generating and saving the image earlier $img->writeImage($path).
you can try to display the image by this way:
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />";
Embedding an image using base64 is a COMPLETELY wrong way to go about the problem esp. with something stateless like a php web script.
You should instead use http parameters to have a single php file which can perform two tasks - the default will send html , and the parameter will instruct the php file to print the image. Below is the "standard" way to do it -
<?php
if (!array_key_exists('display',$_GET))
{
print('<html><head></head><body><img src="'.$_SERVER['PHP_SELF'].'?display=image"></body></html>');
} else
{
// The display key exists which means we want to display an image
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
}
?>
You can embed the raw image in you page, see the blog entry below for an example in page syntax.
http://www.sveinbjorn.org/news/2005-11-28-02-39-23
But i think it would be more productive to save the thumbnail on the filesystem and serve it as normal file. Otherwise you will be generating the thumbnail each time the page is accessed. Someone possibly uploaded this PDF file, so you may as well generate the thumbnail on upload time.
As I can see there are too many answers which are not accurate enough, so here goes mine:
This will print the image as you are doing it now(by the time of asking this question). As alternative to answer by #Vasil Dakov you should modify the snippet i gave you like this:
<?php
// ... Image generation goes here
header("Content-Type: image/jpeg");
ob_start();
print $im->getImageBlob();
$the_outputted_image = ob_get_flush();
?>
// Assuming that you use MVC approach and you are storing $the_outputted_image in a object and passing it to the view(ie. index.html or the HTML below the code).
//... Html code of index.html
<img src="data:image/jpg;base64 <?php print $the_outputted_image; ?>" alt="image" title="IMagick Generated Image" />
As another alternative is creating a script to generate the image, save it in some folder ( assuming img/ is the folder) and return only the path+filename+ extension to the file:
<?php
// ... Image generation goes here
header("Content-Type: image/jpeg");
$filename = 'img/' . md5(microtime()) . '.jpg'// Microtime is just as an example, you should use your own method.
$fp = fopen($filename, "x"); //Creating and opening the file for write-only
$im->writeImageFile($fp); //Writing the image to the file pointer (I would recommend writing it using, fwrite(), because it is binary-safe writing method)
fclose($fp);
?>
// Html
<img src="<?php print $filename; ?>" alt="image" title="IMagick Generated Image" />
documentation for Imagick::writeImageFile
In my case I found out a solution like this:
$im = new Imagick("http://www.yourserver.com/upload/file_name.pdf");
$im->setResolution(300, 300); // if higher image will be good to read
$im->setIteratorIndex(0); // read first page
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
ob_start();
print $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />"; //output as image
good luck.
The only solution would be to convert your image to base64 and include it as an embedded base64 image (data:image/png;base64, ). Further reference.
But this isn't supported in IE 6 and 7.