Laravel - Upload PDF from string - php

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?

Related

Php header() User Agent Change

$file_name = $_GET['title'];
$file_url = $_GET['url'] . $file_name;
header('Content-Type: video/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
I'm using this code to download files in my site fetching from another websites.
It works if my url looks like:-
https://www.example.com/video_download.php?title=video.mp4&url=http://googlevideo.com/video/download/223689/289048
(example)
So, it starts downloading by fetching the video file from http://www.googlevideo.com/video/play/221589 to my site.
But my problem is that the file can be accessed if the person uses a PC.
Can I change the User Agent by using header()?
Is it possible?
So if I change the user agent into a PC user agent, so it can be downloaded from a mobile!
I'm sorry, but the User Agent has nothing to do with readfile() function. Readfile() will just throw the raw file input into your browser. Useful for e.g. rendering images through PHP to the client without having to expose the real file name.
Indeed, it is possible to render video to the client with readfile(), but using a HTML5 video tag will dramatically improve performance. This will also provide better mobile support.
Hope this helps you,
You can use stream_compy_to_stream
$video = fopen($file_url);
$file = fopen('videos/' . $title . '.mp4', 'w');
stream_copy_to_stream($video, $file); //copy it to the file
fclose($video);
fclose($file);
I wrote a class for downloading youtube video files. you can find it here.

How to save as xls from mysql fetching data to www/data/ folder using php?

I am new to PHP development. I am creating excel.xls using the mentioned header information, whereas it is saving as xls at default download folder. Actually, when I click export link, this test.xls file should be saved to www/data/ folder instead of downloading it.
How can I give path for new location to save file using PHP dynamically?
code as follows:
$file = "test.xls";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=$file");
code end
Please help if possible.
Thanks in advance.
Best regards,
Balraj
You can use file_put_contents in PHP.
$file = "path/test.xls";
$data = "Keep your data into this variable";
file_put_contents($file,$data);
file_put_contents is wrapper for fopen,fwrite,fclose.For more information check here

CakePHP read PDF generated and send it as bytes

In my webapp, I can generate pdf file from multiple odt files using gedooo on demand when the user go to this url: http://{base_url}/models/generer/{doc_id}, the modelsController generate the pdf and launch the download on the navigator.
Now I have to send to a distant application by webservice the content of pdf (like an attachment in email) from a model. The problem is to execute the pdf generation and integrate it in my request without store it on the server as file. I tried to do something like:
$pdf = new File('http://{base_url}/models/generer/{doc_id}');
$content = $pdf->read();
And I get an error. I am out of ideas, I need your help.
You can not transmit PDF (binary file).
if you want to transmit it in webservice, you have to convert it to base64
$file = file_get_contents('http://{base_url}/models/generer/{doc_id}');
$filedata = base64_encode($file);
now you can send this data in webservice
echo json_encode(array('file'=>$filedata));
I found the solution!
$this->requestAction($url);
that write the file in the folder of my webapp and
$content = file_get_contents($local_url)

Codeigniter - How to convert a html page to an image?

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.

dompdf Customization Question

I'm trying to optimize dompdf to do something a bit strange. As it is on my server, dompdf is generating a pdf file (for the client requesting the file) from a php/html file stored somewhere on the server. This is cool because it doesn't bog the server down with pdf files, but the problem I have is that I want someone to be able to export a group of PDFs and receive them in a zip file or something similar.
Is there a way to make dompdf export a group of PDF files, based on the filenames of the php/html files, to a zip file or something so the person requesting it can download it?
Let me know if more information is needed.
Thank you!!
DOMPDF only handles a single file at a time. But you could write a PHP script to accept the list of files and then use DOMPDF to parse each one separately. Since DOMPDF can return the rendered PDF as a string you could write each file out to a temporary directory then archive the results when you're done. Or, if you're using dompdf.php you could use exec() to process each HTML document in a similar loop.
Here's a simple example showing one way to do what you want:
$files_html = array('docs/file1.html','docs/file2.html');
foreach ($files_html as $file) {
exec('dompdf.php ' . $file);
}
$zip = new ZipArchive();
$zip->open('docs/pdfs.zip', ZipArchive::CREATE);
$files_pdf = glob('docs/*.pdf');
foreach ($files_pdf as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=pdfs.zip);
header('Content-Transfer-Encoding: binary');
readfile('docs/pdfs.zip');
There are some discussions of using DOMPDF to batch process files in the forum and issue tracker.

Categories