I created a image using imagettftext() function. Now I want to convert the resulted image to pdf. How can I achieve this?
header('content-type:application/octet-stream');
header("Content-Disposition: attachment; filename=certificate.jpg");
imagettftext($image,$font_size,$angle,$x_axis,$y_axis , $temp,$color,$font, $description);
imagejpeg($image);
$imagedestroy($image);
return $image;
Use DomPDf Package.
Add your Image in tag in laravel blade view file. then.
public function generatePDF()
{
$data = ['title' => 'Welcome to codeplaners.com'];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('codeplaners.pdf');
}
Related
My code doesn't save pdf's, I need to show it. If it's possible download it too.
<td><a name="Manual" href="{{asset('storage/app').'/'.$machineData->getManual()}}">
Manual de {{$machineData->getMachineName()}}</a></td>
On controller I have this
protected function downloadFile($src)
{
if(is_file($src))
{
$finfo=finfo_open(FILEINFO_MIME_TYPE);
$content_type=finfo_file($finfo,$src);
finfo_open($finfo);
$file_name=basename($src).PHP_EOL;
$size=filesize($src);
header("Content-Type: $content_type");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
return true;
}
else
{
return false;
}
}
public function download(Request $request)
{
$note=$request->file('manual');
if(!$this->downloadFile(app_path(). $note))
{
return redirect("/machineIndex");
}
}
Please help, I just want to show the file, thanks!!
I believe you can achieve this with TCPDF
$pdf = new TCPDF('P', 'in', [$sizeX, $sizeY], true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->SetMargins(0, 0, 0, true);
$pdf->SetAutoPageBreak(false, 0);
$pdf->Output($outputPath, 'I');
This code would create a new pdf document, then have a look at https://tcpdf.org/ to make the necessary changes and the $pdf->Output($outputPath, 'I'); at the end will open the pdf without saving it to storage (almost like a dd).
Edit:
I wrote this with the assumption that you didn't want to save the file to storage, if the file is already saved in public/storage you can present it with a direct url to it.
You would need the symlink with php artisan storage:link then guide to where it is in the directory. For example 'laravel.test/public/pdfs/pdf1.pdf'
More info on https://laravel.com/docs/8.x/filesystem
I want to preview and download an order invoice pdf using this code :
public function generatePDFByIdOrder()
{
$order = new Order(1); //I want to download the invoice PDF of $order_id '1'
if (!Validate::isLoadedObject($order)) {
throw new PrestaShopException('Can\'t load Order object');
}
$order_invoice_collection = $order->getInvoicesCollection();
$this->generatePDF($order_invoice_collection, PDF::TEMPLATE_DELIVERY_SLIP);
}
public function generatePDF($object, $template)
{
$pdf = new PDF($object, $template, Context::getContext()->smarty);
$pdf->render();
}
And calling it with the following code :
$order = new order();
echo $order->generatePDFByIdOrder();
I have the pdf's data printed on the browser console but not downloaded .
How can I manipulate that data to download a pdf file ?
PrestaShop use TCPDF.
Edit generatePDF in this way:
public function generatePDF($object, $template)
{
$pdf = new PDF($object, $template, Context::getContext()->smarty);
$pdf->Output('name.pdf', 'I');
}
I guess you only have to set the proper headers before rendering the PDF with TCPDF like so :
header("Content-type:application/pdf");
But "downloading" a PDF will depend on what the user's browser settings. It might download them (in which case you'd have to set another header called Content-Disposition:attachment) or display it inside the browser.
We recommend you, to create a separate controller to render the PDF file and to always open that controller in a new tab. It will help you to have separate logic using DOMPDF library.
Invoice controller will be as follows (invoice.php)
include_once(_PS_MODULE_DIR_.'supercehckout/libraries/dompdf/dompdf_config.inc.php');
class SuperCheckoutInvoiceModuleFrontController extends ModuleFrontController
{
public function initContent()
{
parent::initContent();
$this->generateInvoice(ORDER_ID);
}
}
Note: SuperCheckout is the example module name.
generateInvoice() function will be as follows:
function generateInvoice($order_id)
{
$dompdf = new DOMPDF();
$html = utf8_decode(INVOICE_HTML);
$dompdf->load_html(INVOICE_HTML);
$dompdf->render();
}
I use this code to show an image in php
header('Content-Type: image/jpeg');
$img = imagecreatefromjpeg('test.jpg');
imagejpeg($img);
imagedestroy($img);
It is working in a simple php file, but it's not working in a codeigniter controller. I'm using this method:
public function render_image() {
header("Content-type: image/jpeg");
$img = imagecreatefromjpeg('uploads/test.jpg');
imagejpeg($img);
imagedestroy($img);
}
I also tried using this code, but its not helping:
$computedImage = 'path/to/the/image.ext';
$this->load->helper('file');
$this->output->set_content_type(get_mime_by_extension($computedImage))->set_output(file_get_contents($computedImage));
How do I solve this problem?
It was because of the hook. i'm using "post_controller_constructor" hook. i put my code in __construct() and everything is working.
I have actually got very far but my output is gibberish and not a formatted pdf as I expected. Here is the relevant code:
JobsController:
public function viewpdf() {
App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
$this->layout = 'pdf'; //this will use the pdf.ctp layout
$this->set('fpdf', new FPDF('P','mm','A4'));
$this->set('data', 'Hello, PDF world');
$this->render('pdf');
}
View/Layouts/pdf.ctp:
<?php
header('Content-Disposition: attachment; filename="downloaded.pdf"');
echo $content_for_layout;
?>
View/Jobs/pdf.ctp:
<?php
$fpdf->AddPage();
$fpdf->SetFont('Arial','B',16);
$fpdf->Cell(40,10,$data);
$fpdf->Output();
?>
with FPDF installed in the root/Vendors directory.
Change the response type in your controller function:
public function viewpdf() {
App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
$this->layout = 'pdf'; //this will use the pdf.ctp layout
$this->response->type('pdf');
$this->set('fpdf', new FPDF('P','mm','A4'));
$this->set('data', 'Hello, PDF world');
$this->render('pdf');
}
In cake2 you can use a cleaner approach - with or without the new response object:
http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2/
The headers will be set by cake itself from the controller.
It's not good coding (anymore) to set them in the layout (I used to do that, as well^^).
These days the CakePdf plugin has matured quite a bit into a very useful tool for PDF generation.
I recommend using that in Cake2.x.
See http://www.dereuromark.de/2014/04/08/generating-pdfs-with-cakephp
I am using dompdf to create a pdf file out of an html file that gets created on-the-fly for the sole purpose of it serving as input for the pdf generator, however I am having trouble doing this, I implemented the code in this thread and everything works fine (I could output a simple pdf) however when I try to give it a more specific url I get this error:
An Error Was Encountered Unable to
load the requested file
here's the code that has the problem:
function printPDF(){
//write_file() usa un helper (file)
$this->load->library('table');
$this->load->plugin('to_pdf');
// page info here, db calls, etc.
$query = $this->db->get('producto');
$data['table'] = $this->table->generate($query);
$path_url = base_url().'print/existencias.html';
write_file($path_url, $data['table']);
$html = $this->load->view($path_url, 'consulta', true);
pdf_create($html, 'consulta');
}
Not sure about the exact problem, but please check this:
1) as stated in CI's manual, load->view's second parameter should be an associative array or an objet, translated to vars via extract. That may generate some problem generating $html.
2) try making $path_url relative to application/views directory, as read in CI's manual.
you should use tcpdf to create a pdf.
//create controller for example :
public function create_pdf()
{
$this->load->library("Pdf");
$data['results'] = // your data
$this->load->view('pdfview',$data);
}
//pdfview is the page having tcpdf code and your pdf code.
You can try it like this
public function export_pdf() {
$filename = 'FILENAME.pdf';
header("Content-Description: Advertise Report");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/pdf; ");
$file = fopen('php://output', 'w');
$header = array("Question", "Answer", "Name", "Email", "Phone", "Date");
fputpdf($file, $header);
fputpdf($file, 'YOUR DATA');
fclose($file);
exit;
}