I am trying to create a pdf file from html in my codeigniter project. The html file contains an image but it will not load in the pdf. I am using html2pdf library and here is my code.
example.php :
<img src="<?php echo base_url()?>assets/images/logo.jpg" alt="BulkSMS">
I am pasting only the <img> tag here, because it is a large file.
and here is my controller function
public function htmlToPdf($data) {
$this->html2pdf->folder(PURCHASE_INVOICE_PATH);
$fileName = $data['invoiceNumber'].'.pdf';
$this->html2pdf->filename($fileName);
$this->html2pdf->paper('a4', 'portrait');
$content = $this->load->view('templates/example', '', true);
//Load html view
$this->html2pdf->html($content);
$this->html2pdf->create('save');
return $fileName;
}
I tried to change path of image to full path and type of image from png to jpg. But still it not showing
This issue is happening becuase of CORS. Uncomment The following line from the dompdf_config.inc.php file located in application/libraries/dompdf folder.
define("DOMPDF_ENABLE_REMOTE", true);
Related
I am trying to generate a PDF file using DOMPDF in a Laravel project.
The image is displayed correctly when generating my Blade template. On the other hand, the image is not displayed in the generation of the PDF file (the PDF file uses the Blade template).
The controller
$pdf = domPDF::loadView($blade, compact('cv', 'user', 'cv_certifications', 'cv_skills_nom', 'experiences', 'formations','perso', 'adresse', 'region', 'region_select', 'cv_skills', 'tcontrat'));
// Viewing the PDF file in the browser viewer
return $pdf->stream(); // display
The template
<img src="{{URL::asset($perso[0]->cvp_photo_path)}}" height="auto" width="150">
What I want
I would like the image to display in the Generated PDF file
What I get
I obit no errors in the server logs nor in laravel. The image does not load into the PDF file but loads into the rendered template correctly.
Thank you in advance for your help.
I'm trying to use mpdf and it works fine with text but when I try to add an image to my pdf file the page is infinitely loading and nothing happens. I just see the browser page.
When I delete the image, it works fine again and pdf file is created and could be downloaded.
The path to the image is right, I checked on the other simple view page without mpdf.
Here is current result that I see when add an image:
(https://i.ibb.co/KyfmvpN/2019-07-16-23-39-14.png)
I will be glad for any help, please!
Here is the controller:
public function actionCreateMPDF()
{
$mpdf = new mPDF();
$mpdf->WriteHTML($this->renderPartial('mpdf'));
$mpdf->Output();
exit;
}
public function actionForceDownloadPdf()
{
$mpdf = new mPDF();
$mpdf->WriteHTML($this->renderPartial('mpdf'));
$mpdf->Output('MyPDF.pdf', 'D');
exit;
}
and the view file where I am creating my pdf mpdf.php:
<p>Hello</p>
<img src="/images/top.png">
I fixed this problem by deleting the width:100% from the div(where img tag was located) style and putting it directly to the img tag styling.
I'm trying to generate pdf file with laravel and dompdf lib. Here is my code from controller:
public function create(Request $request) {
$pdf = new Dompdf();
$html = Storage::disk('local')->get('public/pdf/template.html');
$pdf->loadHtml($html, 'UTF-8');
$pdf->setPaper('A4', 'portrait');
$pdf->render();
$filename = "Hi!";
return $pdf->stream($filename, ["Attachment" => false]);
}
And in template.html I got some html, somewhere there I got images like this:
<img src="img/logo.png" alt="BTS">
But dompdf cant write this image: Image not found or type unknown. My file system is:
.pdf
...img
......here is images
...template.html
...*some_fonts_files*
But at the same time, fonts are loading fine. What should I do to render images in template.html?
I believe that for dompdf you need to pass a properly accessable url rather than relative paths.
Try
<img src="{{ asset('img/logo.png')}}" alt="BTS">
Alternatively if asset doesn't work please try
<img src="{{ public_path('img/logo.png')}}" alt="BTS">
Also please note that asset uses the public directory as the base_url to calculate the 'asset' from.
Me too i struggled for the same error for 3 days. Also when i tried tricks mentioned above, none of them worked too. But when i changed the image file to .jpg file(extension) everything worked well.
<img src="{{ public_path('img/logo.jpg') }}" >
Laravel version: 5.8.38
PHP version: 7.3
barryvdh/laravel-dompdf version: 0.8.6
You can just print the image path as the src attribute of the <img> tag.
Suppose you have the path to the location of your image in a variable:
$img = asset($imgen);
Yust echo it out like this:
<img src="#php echo $img #endphp">
There seems to be a need to set the protocol and add an additional option:
$pdf->setOptions(['isRemoteEnabled' => true]);
$pdf->getDomPDF()->setProtocol($_SERVER['DOCUMENT_ROOT'])
Or:
$pdf->setProtocol($_SERVER['DOCUMENT_ROOT']);
After this src="/storage/myimage.png" will work.
I tried all tips!! The only one solution that works for me is:
header {
background-image: url("https://example.org/logo.jpg");
}
and
$pdf->setOptions(['isRemoteEnabled' => true]);
$pdf->getDomPDF()->setProtocol($_SERVER['DOCUMENT_ROOT'])
In php code I use mPDF library for generating PDF.
I am facing a problem with insert svg into PDF
The svg file I catch via POST:
$svg = $_POST['structureSVG'];
$svg_pdf = str_replace('"', '\'', $svg); //change " to '
proof what is inside:
var_dump($svg_pdf);
//shows string which contains:
<svg>...</svg>
here I can be sure, that the SVG was captured correctly. So I put the SVG:
$html = " <div> $svg_pdf </div> ";
$mpdf -> WriteHTML($html);
$mpdf -> Output('pdf/test_svg.pdf', 'I');
but unfortunately the SVG picture is not rendered in PDF
thank You for any help
You need to handle the SVG as regular <img> tag. From my point of view, it'd be easiest to save the file to the temp directory and then let it load with mPDF:
<img src="path/to/the/temp/file.svg">
Unfortunately, you can not use data uri for SVG images (at least in version 5.7.4).
I have a form where users can fill input boxes. In some cases, they're allowed to select images too.
The problem is, I don't want to save the image file somewhere on our server and give PDF the URL like this:
//Put uploaded file on public/uploads/ folder
<img src="{{ $imagePath }}" style="height: 50px; width: 50px;">
Instead, I'd like to inject uploaded image directly into PDF file. Something like this:
PDF::load($data, 'A4', 'portrait')->inject('image', Input::file('resim'))->output();
//So I can reference it as {{ $image }} on PDF template, but image will be injected into PDF file
I'm beginner on PDF files and I'm not even sure if this is possible.
If it's possible, can anyone give me some advices?
Ps. I use DOMPDF package for Laravel 4. https://github.com/thujohn/pdf-l4
You will need to be able to reference the image somehow in your document. There are a few ways you could do this.
Access the temporary file from the uploads.
In your HTML file just reference the temporary file created by the upload as your image source, e.g.
<img src="<?php echo $_FILES['userfile']['tmp_name']; ?>">
Capture the content of the image and write it into the HTML as a data-uri value
dompdf understands data-uris and so you could take the uploaded file, convert it to a data-uri, and insert that into your HTML.
You do have to detect the image format, which can require a bit more work depending on your version of PHP. With v5.3+ you can use the finfo_file method. If that extension isn't available you can perform some basic content-type detection by parsing the file extension.
In my example I'm just specifying PNG as the image type.
<?php
// detect mime type, but I'm forcing PNG
$mime = 'image/png';
?>
<img src="<?php echo 'data:' , $mime , ';base64,' , base64_encode( file_get_contents( $_FILES['userfile']['tmp_name'] ) ); ?>">
I have this in my HTML...
<body>
<img src="{{ URL::asset('img/my-image.png') }}" />
my route where i have my img
and this in my controller
public function getPdf($id){
$html = View::make("backend.PDF.batch")->with('batchs',$batchs)->with('ad',$ad)->render();
return PDF::load($html, 'A4', 'portrait')->show();
}
and in the view don't show me the img they say
DOMPDF_ENABLE_REMOTE is set to FALSE