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.
Related
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);
i am using codeigniter i am working on invoice and i want to print a PDF file of my invoice page here is my controller.
$pdfFilePath ='invoice.pdf';
$data=$this->singleinvoice($invoiceId);
ini_set('memory_limit','32M');
$html = $this->load->view('invoice/invoicetopdf', $data, true);
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822));
$pdf->WriteHTML($html);
$pdf->Output($pdfFilePath, 'D');
redirect("invoice");
when i echo $html veriable is show fine but when it output the pdf .it has no css styling i mean it convert the HTML without any of my css Style.
i also tried the following but no luck.
$style=file_get_contents(base_url().'_assets/css/bootstrap.css');
$pdf->WriteHTML($style,1);
$pdf->Output($pdfFilePath, 'D');
Now i want to convert my HTML page to PDF as it is shown to the public with complete styling and markups.
does it possible any suggestion or solution ?
you have to convert your Page Structure from DIV to Traditional Table structure.
arrange your elements contents etc.into table or tables.
then try some basic CSS.
because most of CSS are not supported in mpdf or any other pdf converter.
Click here
to know what(CSS) is supported and what is not in mpdf.
I am generating pdf from html source using mpdf library in php and everything seems working perfect.
Now I have an issue with the images. Suppose before page end I'm inserting an image but image is big so that it doesn't fit at the bottom of first page and goes to second page. Now I have a long white space at the end of first page because image moved to second page.
Now I want is "if next item to insert in pdf is an image then calculate the remaining size of pdf page if it is less than Image size then adjust the image size so that image can be fit in the pdf page instead of moving to next page" how can i do this here?
Please check the issue image :
If any one has other solution please help me to sort out.
Here's My sample code
include_once 'simple_html_dom.php'; //import html dom and mpdf library
include 'PDFScript/MPDF/mpdf.php';
$mpdf = new mPDF('','','','',15,15,30,15,8,8); //create mpdf object
$html = new simple_html_dom(); //create html dom object
$html = file_get_html("htmlsource.html"); //htmlsource.html is a webpage can contain any html data
$mpdf->WriteHTML($html); //write html source to pdf
$mpdf->Output(); //generate pdf
I got a solution from mpdf forum itself. If Anyone also has the same problem enclose every image in your html inside table as table has a autosize feature in mpdf library.
For more information please check here
i'm having the next problem:
I try to see a pdf in one magento phtml this is my code:
$fileName = Mage::getConfig()->getOptions()->getMediaDir()."/pdf/Gutschein-v2.pdf";
$pdf = new FPDI();
$pdf->addPage();
$pdf->setSourceFile($fileName);
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 10, 10, 100);
// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
//ob_start();
$pdf->Output();
//ob_end_flush();
When i comment ob_start(); i see on my screen the next error:
FPDF error: Some data has already been output, can't send PDF file
When i don't comment that i see the normal pdf format but not the white page with
my pdf, i tried to do that with pure php and everything was ok example:
http://milton.bommelme.com/fpdf/pddf.php
but with magento something are not gut, maybe i don't know how to load the pdf or something else. I'm very new with magento.
thank you.
The error "Some data has already been output, can't send PDF file" appears because Magento has already sent the header output. Look at http://php.net/manual/en/function.header.php for further informations.
So the Output() function of FPDF is also sending header informations and it declares the 'Content-Type' to 'application/pdf'. The main problem here is that you cannot just put a PDF file between HTML tags. PDF is an own format and needs a diffrent presantation mechanism than HTML. Like in the example link you gave the PDF file is embeded by the embed-tag with the right Content-Type declaration:
<embed width="100%" height="100%" name="plugin" src="http://milton.bommelme.com/fpdf/pddf.php" type="application/pdf">
There are also other ways to embed a PDF file in HTML: Recommended way to embed PDF in HTML?
EDIT: For example you can create two view actions. The first one renders the HTML. Inside the HTML the embed-tag calls the second action which will generate the PDF.
I don't think this is the best solution,but it should work easily:
class Foo_Pdf_Controller_SomeController extends Mage_Core_Controller_Front_Action
{
public function viewAction()
{
// View stuff
}
public function getPdfAction()
{
// create pdf
$fpdf->output();
}
}
The embed-tags calls the getPdf() action:
<embed width="100%" height="100%" name="plugin" src="url_to_getPdf_action" type="application/pdf">
I have made HTML to convert it into PDF using MPDF,
but issue is that I can't include a stylesheet file.
Example:
include("../mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetDisplayMode('fullpage');
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyleA4.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();
I used the code above but I can't see the effect of the styles.
Does anyone know a better way to include styles?
You need to load the stylesheet in the html - it's not clear in your snippet where the content for $html comes from but that's the place to load styles. Using your code you'll be writing the CSS you've loaded as if it were document content for the PDF, which I assume isn't your intent.
mPdf works best with inline style sheets which cause no bugs while pdf opens in different browsers.
It looks to me you are setting the new PDF function into "Code" mode.
$mpdf=new mPDF('c');
Have you tried it without the "C"?
$mpdf=new mPDF();
Also, is your stylesheet, "mpdfstyleA4.css", located in the same path as mpdf.php?