pdf generator from html in php using Wkhtmltopdf - php

On my windows with xamp local server I follow pdf generator from html
step 1 & 2 (wkhtmltopdf http://www.test.com test.pdf) goes fine but step 3
3. In php you should execute the tool using shell_exec:
shell_exec("/usr/local/bin/wkhtmltopdf-i386 http://test.com test.pdf");
//for Linux 32 bits operating system
//for Linux 64 bits operating system use wkhtmltopdf-amd64
//for windows just put the path of the exe file.
$allfile = file_get_contents("test.pdf");
header('Content-Type: application/pdf');
header('Content-Length: '.strlen($allfile));
header('Content-Disposition: inline; filename="test.pdf"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
does not do any thing... :(
I installed at e drive and used:
exec("e:\wkhtmltopdf\wkhtmltopdf.exe http://www.google.com/ test.pdf");
Actually I creates PDF correctly but does not send output to browser..
any idea what I had missed ...?

I'm guessing but the issue might be file locations. Does file_get_contents actually find the file?
I'm not very sure where the pdf file gets generated during the exec, try to use full paths for both the output pdf location and the file_get_contents location, they might not be correct by default. Maby even check somehow that the files exist and what folder are they both using (I cant remember how to do this in PHP :))

Related

PHP headers force download on Internet Explorer 11 won't skip pop up

I have these PHP headers that will force a download (Intranet site) that works on Chrome without prompting the popup, but I cannot avoid the popup warning in Internet Explorer 11 (Version 11.657.18362.0) on Windows 10.
I've tried adding to IE "intranet" site and "internet" site the internal server address this would run from (http://[server]), lower the security levels to the lowest settings for this zone under "intranet" and also "internet". Close and open Internet Explorer and it still has the popup warning.
Here is my PHP code below:
if ($_REQUEST['c']){
$num = $_REQUEST['c'];
$cmd = "test.exe $num";
$ctype ="application/octet-stream";
ob_end_clean();
header('Content-Description: File Transfer');
header('Content-Type:' . $ctype);
header('Content-Disposition: attachment; filename="test.bat"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($cmd));
echo($cmd);
exit;
}
Additionally, once the bat file has downloaded, there is a separate AutoIt script on the user's computer that will scan the download folder for this specific file and will automatically run the file and delete it without the user having to do anything. The idea is to limit the user's clicking to just one time.
This is the popup that I get from IE when I click to download the bat file below:
Any ideas what's wrong or other suggestions around this?
I'm able to reproduce the problem, but it seems that IE browser will keep display this prompt when downloading the bat file for security reason, and we can't disable it.
As a workaround, I suggest you could convert the bat file to a ZIP file, and then download it. Here are some related threads about how to Zip files using JavaScript, you could check them:
How to create zip files using javascript?
How to Zip files using JavaScript?

accessing a jpg which is not in the web folder through PHP

I recently found a youtube video that talked about accessing a PDF in a web browser. It was interesting because as I suspected I could access a PDF most anywhere on the system (given permissions) and pass it along via PHP to a web browser.
I do not want his to become a security discussion so please abstain from security comments!
https://www.youtube.com/watch?v=z4a6QJSGL28
The code looks something like this
$file="/home/kodi/Pictures/scans/test.pdf";
$filename="test.pdf";
header('Content-type: application/pdf');
header('Content-disposition: inline; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
#readfile($file);
So I tested and it works then I tried this
$file="/home/kodi/Pictures/scans/test.jpg";
$filename="test.jpg";
header('Content-type: application/jpg');
header('Content-disposition: inline; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
#readfile($file);
Oddly enough this did NOT display in a web browser, but several seconds later, my preferred app for downloaded jpgs opened and there was the file . It had downloaded instead of opening in browser.
I have been working on a LAN app that can access users folders on the server through PAM authentication and have been using base64 encoding, however I felt that if I could reduce the overhead of base64 images (30%) , conversions and literally converting each image in a list to display links to them, the system would load faster. This would definately be a better way but everyone says that this kind of thing does not work! The proof that there is hope that it will work ios in the file I downloaded from the web server that was in a user folder.
Any ideas how to make it work with jpgs?
Your content type is incorrect, it should be image/jpeg, instead of application/jpg

MP4 Video not playing in Quicktime on MAC after passing it through php readfile()

I've spent days searching for a solution to this problem but nothing I try works. Situation is as follows: we host a number of videos on Cloudfront and stream those through our website. (works like a charm)
Premium members need to be given the option to download some of the videos, however, we cannot show the actual URL. For that reason, we've set up the following script, which reads the video from our Cloudfront URL and passes it through the server, that way effectively hiding its location.
Here's the weird part: Although this works fine for some videos, others (with the same encoding) won't play in Apple's Quicktime player, saying that the format is not recognized. If I open the file in VLC it works perfectly fine but Apple is acting up.
Naturally I tried to download the file directly from its URL, and it works just fine in Quicktime. Also, the headers are the same in both, the direct download, and the readfile passthru.
Strangly, though, as I said, other videos with lower resolution work fine using the script. I could not find any reason why resolution should have an effect on this, though. Everything else is equal. Videos are encoded in the H.264 format and are stored in a MP4 container.
Any thoughts on what I can do to fix this issue?
Thanks so much
header('Content-Description: File Transfer');
header('Content-Type: video/mp4'); //tried multiple options here, none work
header('Content-Disposition: attachment; filename=asciifriendly-filename.mp4');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: 1234567890'); //this number IS correct. 100%
ob_clean();
flush();
readfile ( $url ); //points to Cloudfront server

Using JavaScript and PHP to download image files and return a zip file

I'm in the middle of developing a Safari extension for imageboard-type websites and one of the bigger features I'm hoping to implement is the ability to download all of the images (the posted ones, not the global page-level images) that had been posted.
There are similar questions here already, but mine differs a bit in that the images in question are hosted on an entirely different server. I've been brainstorming a bit and figured that gathering all of the image URLs in a JS array then sending it to my server to be turned into a zip file (forcing the download, not just a link to the file) would be the best way to go. I also want the zip to be deleted after the user downloads it.
I've already finished the majority of the extension features but this one is stumping me. Any help would be greatly appreciated.
How would I'd go about doing this?
You want a extension to contact your server for downloads? That's a terrible idea! Make the zipfile locally - it's not regular javascript, it's an extension - you have full access.
Anyway assuming you want to do this anyway, what is the trouble you are having? You get a list of urls, send them to your server, your server downloads them, zips them and send them to the user. (The "your server downloads them" part should worry you!)
What problem are you having?
You can use PHP's ZipArchive class to make a ZIP, then stream it to the browser.
<?php
// Create temp zip file
$zip = new ZipArchive;
$temp = tempnam(sys_get_temp_dir(), 'zip');
$zip->open($temp);
// Add files
$zip->addFromString('file.jpg', file_get_contents('http://path/to/file.jpg'));
$zip->addFile('/this/is/my/file.txt');
// Write temp file
$zip->close();
// Stream file to browser
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myFile.zip');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($temp));
readfile($temp);
unlink($temp);
exit;

Open/save PDF located in server folder

I want to give to the users of a PHP intranet the possibility to open/save PDF files, which are located in a folder on the Apache server. The PDFs have company private information, so I don't want to put them in a web folder.
echo '<form name="openpdf" method="POST" action="downloadPDF.php">';
echo '<input type="hidden" name="pdf">';
echo'</form>';
<tr>
<td> PDFFile1 </td>
<td></td></tr>
downloadPDF.php:
<?
$pdf=$_POST["pdf"];
if (file_exists($pdf)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($pdf));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pdf));
ob_clean();
flush();
readfile($pdf);
exit;
}
?>
The problem is when the user open/save a file, the path is pointing for that folder but in the client PC and not at the server.
If you process the PDFs internally on the server from PHP, you should omit the file:/// from the URL.
So it should be
$pdf="c:/pdfs/example.pdf";
The server does not know the client PC, so readfile does not work here.
You might want to try with a redirect, but I must admit I have no clue if browsers allow that for security reasons (you're switching protocols with the redirect).
What at the moment you are doing is that
Javascript is putting a value of c:\pdfs\example.pdf in the pdf field of your form on click and submitting it to downloadpdf.php.
On server, Downloadpdf.php is assigning $_POST["pdf"] value to $pdf.
If file $pdf exists, it is simply proceeding to offer user to download this file.
Now, this may work where server & client is same computer(specifically on a PC, because of C: drive); i.e. like on Local XAMPP. But on real world, where user and server will be on totally different computers, the if (file_exists($pdf)) is always going to fail(unless On server's C: actually there is a file exaple.pdf in folder pdfs)
In real world, step 3 will fail, because $pdf = c:\pdfs\example.pdf and server will look into its own C: drive (if it is a windows server).
You should
1 Try to upload file with an HTML File Upload Box.
2 Get/fetch it on server using $_FILES and do processing
3. Send required headers for downloading.
For further information, please see HTML Form File Upload (Google) & $_FILES (PHP.Net)

Categories