I've a webpage, coded on codeigniter framework, which lists all available certificates. Then I can print a certificate by clicking on its name or by checking checkboxes if the user wants to print more than 1 certificates.
Currently what I do is just to view the html of the certificate, and the user had to right click on their browser and click on print. Which is fine for 1 page of certificate.
But when I've 2 or more certificates, the certificates aren't stack perfectly. As far as I know, I can't set the html page to be an A4 size print.
Codeigniter had an image manipulation library. http://codeigniter.com/user_guide/libraries/image_lib.html
But couldn't find what I wanted.
Is there a way to convert these html pages into image which I can later print those? Or should I try other libraries?
I would use DOMPDF to create a PDF document. You can generate them the html as normal, then use the third parameter of the load view function to return the html for the PDF processing.
Something like this in your controller;
$this->load->helper('pdf');
$html = $this->load->view('certificate_view', array(), TRUE);
$attachment_location = $_SERVER["DOCUMENT_ROOT"] . '/certs/'.$title.'.pdf';
pdf_create($html, $title, $stream=FALSE, $orientation='portrait');
// send open/save pdf dialog to user
header('Cache-Control: public'); // needed for i.e.
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.$title.'"');
readfile($attachment_location);
exit;
The Codeigniter DOMPDF library is here.
You can convert an html to image by using this simple tutorial. It uses imagick and no gd library.
Related
I am using Quickbooks PHP SDK to generate a PDF from an Invoice but I would like to upload the PDF to the server. Currently I can request the Invoice and download the PDF but how do I tell Laravel to take the PDF and upload it without it being downloaded?
$invoice = $dataService->FindById("Invoice", "130147");
$pdfContent = $dataService->DownloadPDF($invoice, null, true);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="invoice.pdf"');
echo $pdfContent;
exit();
Instead of it downloading I would like to do something like:
$path = $pdfContent->storeAs('public/pdf', invoice.pdf);
Is this possible?
I did find a solution outside of my question above.
The Quickbooks API allows the ability to pass a file location in the second variable. Also the 3rd variable can be set to true to generate a raw PDF.
$invoice = $dataService->FindById("Estimate", "130147");
$data = $dataService->DownloadPDF($invoice, base_path('/pdf/'), false);
While this is a great simple solution I wonder if there is an solution with how I was originally going about?
I try to make force download html-file to pdf and use the code below
<?php
header('Content-disposition: attachment; filename=\''. basename($file) .'\'.pdf');
header("Content-type: application/pdf");
echo file_get_contents($_SERVER['DOCUMENT_ROOT'].'/index-1.html');
?>
I get some file with .pdf extention but when I try to open it, i get an error `not PDF or corrupted'. What's wrong and how to make it work?
Telling the browser that some data is a PDF will not magically transform the data into a PDF.
You'll need to do that yourself, e.g. using the PHP PDF library or Prince.
You have to create a properly PDF file. You can do this using some PHP classes like HTML2PDF or wkhtmltopdf.
You can't just put html in a pet file and expect it to work you need to use something like dompdf to convert html to a pdf file: https://code.google.com/p/dompdf/
For even better results and support for floats I would use the following library. I got extremely good results with it. The reason why this one is working so well is because they use the WebKit browser rendering engine to render the html before converting it to pdf giving you almost 1 on 1 results when comparing it to the browser.
https://code.google.com/p/wkhtmltopdf/
I am working on a Symfony 1.4 project. I need to make a PDF download link for a (yet to be) generated voucher and I have to say, I am a bit confused. I already have the HTML/CSS for the voucher, I created the download button in the right view, but I don't know where to go from there.
Use Mpdf to create the pdf file
http://www.mpdf1.com/
+1 with wkhtmltopdf
I'd even recommand the snappy library.
If you use composer, you can even get the wkhtmltopdf binaries automatically
Having used wkhtmltopdf for a while I've moved off it as 1) it has some serious bugs and 2) ongoing development has slowed down. I moved over to PhantomJS which is proving to be much better in terms of functionality and effectiveness.
Once you've got something like wkhtmltopdf or PhantomJS on your machine you need to generate the HTML page and pass that along to it. I'll give you an example assuming you use PhantomJS.
Initially set what every request parameters you need to for the template.
$this->getRequest->setParamater([some parameter],[some value]);
Then call the function getPresentation() to generate the HTML from a template. This will return the resulting HTML for a specific module and action.
$html = sfContext::getInstance()->getController()->getPresentation([module],[action]);
You'll need to replace the relative CSS paths with a absolute CSS path in the HTML file. For example by running preg_replace.
$html_replaced = preg_replace('/"\/css/','"'.sfConfig('sf_web_dir').'/css',$html);
Now write the HTML page to file and convert to a PDF.
$fp = fopen('export.html','w+');
fwrite($fp,$html_replaced);
fclose($fp)
exec('/path/to/phantomjs/bin/phantomjs /path/to/phantomjs/examples/rasterize.js /path/to/export.html /path/to/export.pdf "A3");
Now send the PDF to the user:
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Content-Description','File Transfer');
$this->getResponse()->setHttpHeader('Cache-Control','public, must-revalidate, max-age=0');
$this->getResponse()->setHttpHeader('Pragma: public',true);
$this->getResponse()->setHttpHeader('Content-Transfer-Encoding','binary');
$this->getResponse()->setHttpHeader('Content-length',filesize('/path/to/export.pdf'));
$this->getResponse()->setContentType('application/pdf');
$this->getResponse()->setHttpHeader('Content-Disposition','attachment; filename=export.pdf');
$this->getResponse()->setContent(readfile('/path/to/export.pdf'));
$this->getResponse()->sendContent();
You do need to set the headers otherwise the browser does odd things. The filename for the generated HTML file and export should be unique to avoid the situation of two people generating PDF vouchers at the same time clashing. You can use something like sha1(time()) to add a randomised hash to a standard name e.g. 'export_'.sha1(time());
Use wkhtmltopdf, if possible. It is by far the best html2pdf converter a php coder can use.
And then do something like this (not tested, but should be pretty close):
public function executeGeneratePdf(sfWebRequest $request)
{
$this->getContext()->getResponse()->clearHttpHeaders();
$html = '*your html content*';
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED, 'whatever_name.pdf');
throw new sfStopException();
}
I am using Yii Framework, TCPDF and jQuery to generate a pdf.
The pdf is generated by inputing in a form and submitting it using ajax.
The pdf is created but here is the problem when it returns to the client, it down not download.
here is the php code
$pdf->Output('Folder Label.pdf','D');
the jQuery on success function has
success: function(data) {
window.open(data);
}
Which i got from this site.
Can you please help
If the problem is that you are not getting the browser's download dialog for the PDF, then the solution is to do it this way:
First, redirect the browser (using window.location as the other answers say) to navigate to a special controller action in your application, e.g. with this url: http://your.application.com/download/pdf/filename.pdf.
Implement the action referenced in the URL like this:
public function actionPdf() {
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="filename.pdf";');
header('Content-Length: '.filesize('path/to/pdf'));
readfile('path/to/pdf');
Yii::app()->end();
}
This will cause the browser to download the file.
You need to save the PDF to somewhere on your server and then issue window.location = '/url/to/pdf-you-just-saved.pdf'; from your javascript. The users browser will then prompt them to download the PDF file.
in tcpdf , just pass this argument the Output method:
$pdf->Output('yourfilename.pdf', 'D');
that's all
Not quite, that will cause errors on some browsers, this is the correct way to set the window location.
window.location.assign( downloadUrlToPdf );
So
Send a request to make the pdf via Ajax to the server
Process and generate the pdf on the server
Return in the Ajax call the url to the file you just made
Use the above code fragment to open a download of said file
i want to make a pdf file with the help of php code if suppose i make a register form after click the submit button that form information must come to pdf file. is there any way to do. i got a one link but that PDF is not working properly
i have been searching but still i can't get the demo.
you guys can any one help me.
thanks
Try FPDF library. I am posting part of code I used for generating pdf dynamically in php.
require_once('fpdf/fpdf.php');
$frontpdf = new FPDF('P', 'mm', 'A4');
$frontpdf->AddPage();
$frontpdf->SetFont('Arial','',12);
$frontpdf->Cell(210, 230.19, $frontpdf->Image('./images/cover.jpg', 0, 0, 205, 230.19));
$frontpdf->Ln(218);
$frontpdf->Cell(110,6,$fname.' '.$lname,0);
$frontpdf->Ln();
$frontpdf->Cell(110,6,date('d.m.Y'),0);
$frontpdf->Output('./pdfs/your_data.pdf', 'F');
For details see http://www.fpdf.org/
Use 'I' or 'D' instead of 'F' in last line if you want to send pdf file instead of saving it. You can use FPDF in php4 as well.
Copied from here:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
Set the action url of your form to a php file like the one above.
You will need a pdf generator and i recommend you to use tcpdf:
http://www.tcpdf.org/
It is open source and when you download the library you have more than 50 or 60 how to use example scripts.
Unlike other open/source libraries i know that this one actually supports utf-8 (depends on the font used). I tested fpdf, dompdf and few others becouse i needed these characters in my documents: č, ć, ž, š, đ and that is the only library i could use for that.
If you need a specific example show me your code and i'll help you :)