Tablet stream (snappy) - php

Laravel 6.0+
https://github.com/barryvdh/laravel-snappy
here is my problem. I would like that on tablet I can print the pdf directly or then be able to stream the pdf as on the pc.
because currently on tablet I have to download the pdf, open the pdf, and print the pdf.
I would like to simplify all that.
public function pdf()
{
$order = Order::first()
$pdf = PDF::loadView('admin/pdf.order', compact('order'));
return $pdf->stream('Pdf_'. $order->num_order .'_'. $order->customer->name);
}
Thank you for your help.

I have found a solution to my problem I am going through an html basic view with a button in javascript which asks to print and which directly starts printing!

Related

PHP PDF download is not working properly in mobile

I am creating a web application in Codeigniter 4 in which users can purchase a PDF book. After payment, the user is redirected to a page where he/she can download PDF directly. The page doesn't have any UI. The PDF download is implemented using headers. The PDF download function is working perfectly on PC, but on mobile, the file is downloading as an HTML file.
eg : filename.pdf.html.
My function is as below
public function download()
{
$file_url = WRITEPATH . 'uploads/file.pdf';
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=filename.pdf");
readfile($file_url);
}
I have searched and tried almost all the solutions. But still, the issue is not resolved in mobile.
Solved the issue.
Looks like Codeigniter needed exit() function after readfile()
Thanks to #vee who gave the tip.

How to use PDFTK background in php?

PHPTK has a option that allows library devs to set a background for the pdf that is generated and here is the link - https://github.com/mikehaertl/php-pdftk#add-background-pdf
But in short it tells to add -
$pdf->background('/path/back.pdf')
But when I use this in my code it doesn't work I mean the back.pdf is not a background inside my generated pdf.
public function generate($pdfdata)
{
$pdfname = 'pdf_'.rand(2000, 1200000).'.pdf';
$pdf = new Pdf("./rawpdf/test.pdf");
$pdf->fillForm($pdfdata)
->flatten()
->needAppearances()
->saveAs("./generated/".$pdfname);
return $pdfname;
}
In my thought process I think If i use the background then it should be behind the text or can be said over the generated pdf but those things don't happen by the way if i am using this not correctly please correct me I new to this library.

Android app webview is not downloading or loading pdf, php mpdf

I have developed a page which display invoice in html and has a download link for PDF. I am using mpdf library- php. Here is my code for download link:
public function downloadInvoice()
{
$id = $this->uri->segment(2);
$where = array('invoice_id'=>$id);
$data['invoice'] = $this->Common_model->getDataWhere("invoices","*",$where);
$mpdf = new \Mpdf\Mpdf();
$this->session->set_userdata($data);
$filename = $invoice['invoice_no'].'.pdf';
header("Content-Disposition", "attachment; filename='.$filename.'");
$html=$this->load->view('user/pdf/download_invoice',[],true);
$mpdf->WriteHTML($html);
$mpdf->Output($filename, 'D');
}
I am able to download the pdf in each and every browser, event in mobile browser. The android app developer using this link in app and showing it in webview. Now he is saying that download link does nothing. I also asked him to try the download link directly . this is also not working. What change should I apply to make it work in mobile. Mobile developer is saying that he has no control on webview and cannot do anything from his side.
Please help.

Codeigniter force_download() does not work for PDF extensions

I'm facing two problems.
force_download() does not work for pdf extensions.
when I try to download a txt extension file the functions work fine
below code.
public function index()
{
$data = "some text";
$name = "sample.txt";
force_download($name,$data);
}
but when I try to do it with a pdf extension, the file gets downloaded properly but when I click the downloaded pdf file it shows Error: Failed to load PDF document.
public function index()
{
$data = "some text for pdf";
$name = "sample.pdf";
force_download($name,$data);
}
secondly is there a way to ask or show a dialogue box before downloading a file instead of force download.
You cannot create a (not currupt) pdf file with force_download(). You'll need to create the pdf file using a php library like fpdf or similar. The force_download() function only generates a header which forces a download to happen.
If the pdf file exists on the server you can force_download('/path/to/my_pdf.pdf', NULL);
concerning the second part of your question, this is too broad, there are thousands of ways to create a dialog box
pass name of the file you want to download
Controller
function download($file_name)
{
$this->load->helper('download');
force_download('./assets/'.$file_name, NULL);
}
View
<a href="<?php echo SURL.'controller_name/download/'.$file_name;?>">Download
</a>
For second question try onclick()

HTML2FPDF print page results as pdf

I'm trying to user HTML2FPDF (http://html2fpdf.sourceforge.net/) to create a PDF of a page, but I can't seem to get it to work properly.
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
http://portal.flyingstartlifestyle.360southclients.com/leanne/leanne.php <- the graph with the html2fpdf code at the bottom of the page.
HTML2FPDF code:
function createPDF() {
define('ABSPATH', dirname(__FILE__).'/');
require(ABSPATH.'classes/pdf/html2fpdf.php');
$pdf = new HTML2FPDF();
$pdf->AddPage();
$html = ob_get_contents();
//$html = htmlspecialchars($html);
if ($html) {
$fileName = "testing.pdf";
$pdf->WriteHTML($html);
$pdf->Output("pdfs/".$fileName);
echo "<p>PDF file is generated successfully! Click here to open it.</p>";
} else {
echo "<p>There has been an error in creating your PDF.</p>";
};
};
If I unhide the line "$html = htmlspecialchars($html);" it prints the pdf the text of the page, otherwise it creates an empty PDF. Any ideas how I can transfer my graph to a PDF?
Cheers
A few years back, I've been beating my head against the wall trying to convert HTML into PDF for days. What I wanted to do was really simple - make an invoice for customers into a PDF file. An image (logo) up on top, a few paragraphs, and a table with a list of charges.
The hole shaped like my head on the wall is still there. All of the free libraries that convert things to PDF - they all suck. I found one that sucks the least, it's DomPDF. At least that one ended up doing the job, after a week of suffering and debugging. It's not fast by any means, though (if you want to generate a complex PDF, you might want to do it off-thread).
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
jQuery is interpreted by the browser and not by the server. When you send the HTML to be rendered into PDF, it will not run the Javascript. You'll need to find a way to actually generate the image some other way.
I guess I could see a situation where you could use ajax to make a remote call and send all of the html that the js sees.
The remote call then would write a file of that html. The remote call would send back a file name for the pdf to be generated.
Your js then could provide a link to the processing page of the html2pdf that references the file created from the remote call.
This would work, but it might be a bit much.
Regards.

Categories