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
Related
I have a code to convert an html text into pdf and another to merge this pdf with a pdf that the user uploads, but I can't merge the two together, it downloads the converted pdf and not the merged one.
When I put just to merge with two files that the user uploads it works.
My code:
$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
$arquivo = $dompdf->stream();
$ilovepdf = new Ilovepdf('iLovePdfKey', 'iLovePdfKey');
// Create a new task
$myTaskMerge = $ilovepdf->newTask('merge');
// Add files to task for upload
$arquivo = $this->convertHello();
$file1 = $myTaskMerge->addFile('path to the file that the user upload');
$file2 = $myTaskMerge->addFile($arquivo);
// Execute the task
$myTaskMerge->execute();
// Download the package files
$myTaskMerge->download();
$dompdf->stream() sends the rendered PDF to the browser. As such you can't access the generated PDF that way. You have to capture the output and save to a file.
Based on your sample code, something like this:
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
$dompdf->render();
$arquivo = $dompdf->output();
$tmp = tempnam(sys_get_temp_dir(), "pdf")
file_put_contents($tmp, $arquivo);
$ilovepdf = new Ilovepdf('iLovePdfKey', 'iLovePdfKey');
$myTaskMerge = $ilovepdf->newTask('merge');
$file1 = $myTaskMerge->addFile('path to the file that the user upload');
$file2 = $myTaskMerge->addFile($tmp);
$myTaskMerge->execute();
unset($tmp);
$myTaskMerge->download();
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 trying to create a .pdf file with a base64 string from an image, and I can create it correctly, but when I try to open the file, the program sends a message that tells the file is corrupt or something like that..
I got this code:
define('UPLOAD_DIR', '../image/');
$img = $_POST['image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$uniqueNumber = uniqid();
$namefile = $uniqueNumber.'.png';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
$namefile = $uniqueNumber.'.pdf';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
I can open the .png file correctly so, i think it's not problem from the base64 decoded string. Thank you all!
EDIT:
I'm trying this code and getting the same issue.
$data = base64_decode ($img);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$data);
//close output file
fclose ($pdf);
echo 'Done';
Is that becouse i'm saving an image with .pdf ? I think no, because if i'm doing fopen with .pdf should be with that format.
EDIT 2:
FOUND A SOLUTION.
http://www.fpdf.org/en/script/script45.php
I followed these steps and i can get that, thank you all!
Check out DOMPDF: https://github.com/dompdf/dompdf
You can definitely use DOMPDF to create a PDF with an image tag whose source is that Base64 string. and render that to PDF.
<?php
require_once("dompdf_config.inc.php");
$img = $_POST['image'];
$html =
'<html><body>'.
'<img src="'.$img.
'"></body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
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'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