I use the FPDF library to display online PDF files, with the ouput option « I » (which output the content to browser).
Then, I need to be able to save some of those generated pdfs on my server (like does the ouput option « F »).
In order to do that, I wrote this script:
$pdf_data = file_get_contents('https:/HOST/Folder/file.php');
$path =__DIR__.'/Folder/file.pdf';
file_put_contents( $path, $pdf_data );
It succeeds to create a file with pdf extension to the directory, but :
- I got an error: « Format error : not a pdf or corrupted » when I try to open the "pdf" file,
- The saved file is empty (?).
So, what is wrong?
How can I get the content generated by the php file, and save it as a pdf on my server?
Finaly, I forgot this script and found a new solution that works!
I set up two outputs into my PHP code build with FPDF, one to display the file online and one to save the file on the server.
The content generated by the PHP code is always displayed, and I control the PDF backup with a string on the first output.
So, when I need to backup, I use a Jquery function to execute the PHP code in the background and upload the PDF file (hidden iframe works too).
Below the end of my PHP code:
<?php
...
if (mycondition) {$string='F';}
else {$string ='I';}
...
$pdf->Output(__DIR__.'/../../Folder/file.pdf',$string); // save on the server
$pdf->Output('file.pdf','I'); // display on the browser
?>
Below the jQuery function I use:
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
var phpfile = <?php echo json_encode($Filename); ?>; // the php file to upload
function uploadpdf() {
$.get(phpfile);
return false; }
</script>
Hope this will help!
Related
I am looking to generate a QR code on a PHP webpage. The user starts on a page where they submit a form, the only input being the name of the new client. This page is a manager for VPN clients.
After the redirect, PHP generates the configuration file on the page to copy and paste. The user must save this file as wg.conf in their etc directory. I have been trying to use qrencode, a Linux based command line tool to generate the QR code on that same page with the configuration file. According to the documentation on qrencode, you can save the file as a .png or .svg.
I have been playing around with it, both trying to pass data through the URL as GET parameters in the redirect (the only issue is the QR code is sensitive) and by using the backtick operators to run bash commands to save it as a file in the user's local files. I can't seem to figure out where to save it as a file or whether there's another solution to display it on the webpage.
how i would do it:
check this library https://github.com/neocotic/qrious , all you need to do it load the js on your page
generate text you want to convert to qr code in a text area, div or other HTML element and assign it an id
use that id to extract the data via pure JS, now you have a text variable and you have the possibility to make qr code dynamic now
once you have the data dynamically extracted via JS you can use that variable to generate qr codes like this :
var x = 'sample text'
var qr = window.qr = new QRious({
element: document.getElementById('qrious'),
size: 200,
value: x
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrious/4.0.2/qrious.min.js"></script>
<canvas id="qrious">
I have a strange question regarding PDF.JS and PHP
Using PDF.JS, to open pdf you should use on browser:
http:// ...url... /viewer.html?file=[filename]
I have changed viewer.html extension to viewer.php and developed an additional file document.php to get data from database and load a local PDF file, and I use in this way to get PDF
... viewer.php?file=document.php&control=5E71581C52B96
All work great , AS ESPECTED, but i'm experiencing trouble when get variables from database.
When I Use fileurl like this, PDF load correctly:
$fileurl = 'D:\Drive\_DEV\storage\220\2020-03-17\00000000002\1584486427900.pdf';
When I use fileurl like this (variables from database) PDF not open and have error
Corrupted or inválid PDF Invalid PDF structure
$fileurl = $storage_path.'\\'.$storage_folder.'\\'.$document;
If I echo, $fileurl in both cases i have exactly same result.
Regarding SQL QUERY below, on WHERE Clause if I change '$doc_control' (from request) with '5E71581C52B96' (instead '$doc_control') PDF load correctly into pdf.js
If I echo $doc_control, number is exactly same, only difference is how put value on WHERE (number or variable)
If i open document.php work with no problems.
What do I Wrong? Any help is very appreciated.
DOCUMENT.PHP
// REQUEST
$doc_control = $_REQUEST['control'];
// Read database
SELECT *
FROM docs
WHERE doc_control = '$doc_control'
// FILE PATH
$fileurl = $storage_path.'\\'.$storage_folder.'\\'.$document;
// READ PDF
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename=".$fileurl);
//#readfile($fileurl);
$file=fopen($fileurl, "r") or die('Unable to open file');
echo fread($file,filesize($fileurl));
fclose($file);
I am new to PHP report generation. I am trying to use MPDF 5.7. When I try create sample pdf using simple php page, it is created successfully. But when I put it in to codeigniter chrome browser says "Failed to load PDF document.". But It can open using firefox. But if i downloaded it, it is not support to open using adobe reader. But still my sample pfd is working well anyway.
This is how I create sample pdf.
<?php
$html = '
<h1>mPDF</h1>
<h2>Basic Example Using CSS Styles</h2>
<p class="breadcrumb">Chapter » Topic</p>
<h3 style="color:red; background-color:gray; margin-left:100px;">Heading 3</h3>';
include("themes/MPDF57/mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetDisplayMode('fullpage');
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyleA4.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
When I put it into codeigniter cannot open pdf. my php file is created in 'view' and mpdf library also put in same place in theme folder.
Your PDF file contains some direct PHP output from your script which makes it corrupt.
It can be a whitespace from after the ?> PHP closing tag, it can be PHP notice printed during course of generating the file, etc.
Be sure that you disable all layouting your framework provides as it produces the exact same output that could cause this.
To troubleshoot further, save your PDF file to disc ($mpdf->Output('<file path>', "D");) and open it in a text editor. You will see a bunch of weird characters.
If the file does not start with %PDF-1.4, look for the reason of the output before calling $mpdf->Output();.
If there is some readable text at the end of the document, look after calling the Output method.
I'd suggest you to remove your ?> PHP closing tag anyway for a good measure.
See also https://mpdf.github.io/troubleshooting/error-messages.html
The reason this happens is that there are some PHP errors/warnings that show up before the PDF gets written, these make it a non-valid PDF therefore not readable by the Browser.
You can see it by downloading and opening the PDF file with a TextEditor and you'll see PHP errors instead of the standard %PDF-1.4 value (which should be the first value of the file).
If you suppress warnings error_reporting(E_ERROR | E_PARSE); you should be good to go.
Try this
$mpdf->Output('', "I");
so then file should be displayed inline in browser instead of downloading it.
if you want to download use "D" insted of "I"
I had the same problem, I didn't notice that header and footer (header was the problem) are still included in my index file. When I called my generate_pdf file it was pulling header and that created error. After I removed header pdf was generated successfully.
I would like to pull data from a mysql db.
This data is then inserted into a html file which is then converted to a pdf using dompdf.
The template is perfect and display's well when I run call dompdf.
However as soon as I try and insert php code, the template still shows perfectly, how ever the php code is displays nothing. If I open the page its shows, so I know it works.
In the options file I have done this :
private $isPhpEnabled = true;
my php file to call the template (LeaseBase.php):
<?php
$options = new Options(); $options->set('isPhpEnabled','true');
$leasefile = file_get_contents("Leases/LeaseBase.php");
$dompdf = new Dompdf($options); $dompdf->loadHtml($leasefile);
$dompdf->stream(); $output = $dompdf->output(); file_put_contents('Leases/NewLeases.pdf', $output);
?>
I also can't seem to pick up anything in the log files.
Any assistance is appreciated
However as soon as I try and insert php code, the template still shows
perfectly, how ever the php code is displays nothing.
Answer: It shows nothing because when a php page is executed, it outputs html (and not the php code). If you don't have an echo or print or any code that generates html code from the php script, the page will in fact be blank.
It's important to remember that php is serverside code WHICH CAN generate html code as long as you instruct it accordingly.
With versions of Dompdf prior to 0.6.1 you could load a PHP document and the PHP would be processed prior to rendering. Starting with version 0.6.1 Dompdf no longer parses PHP at run time. This means that if you have a PHP-based document you have to pre-render it to HTML, which does not happen when using file_get_contents().
You have two options:
First: Use output buffering to capture the rendered PHP.
ob_start();
require "Leases/LeaseBase.php";
$leasefile = ob_get_contents();
ob_end_clean();
Second: Fetch the PHP file via your web server:
$leasefile = file_get_contents('http://example.com/Leases/Leasebase.php');
...though, actually, if I were loading the file into a variable and feeding it to dompdf without doing any further manipulation I would use dompdf to fetch the file instead. In this way you are less likely to have to deal with external resource (images, stylesheets) reference problems:
$dompdf->load_html_file('http://example.com/Leases/Leasebase.php');
I am working with Google Chart API where I am converting the graph into image and then download it into my downloads folder. Now after download I want to rename the image file and move it to other directory for which I am using rename() function in PHP.
Now the problem I am facing is that the rename() function in PHP executes before I can execute the download image function (which is in javascript) and hence it gives me error showing "Specified file not found".
I have tried using PHP delay function usleep() and javascript function setTimeOut() and I also tried "time-wasting" loops. but didn't have any success.
Can Someone please suggest me something I can implement to accomplish this.
This is my code:
/*Firstly there is the google line chart code */
In body I have:
<script type="text/javascript">
onload = function download_this(){
grChartImg.DownloadImage('chart_div');
}
</script>
//PHP
<?
$changefrom = "C:/somelocation/Downloads/download" ;
$changeto = __DIR__.'\mygraph';
rename($changefrom, $changeto.'.png');
?>
This is grchartimg library which convert and download the graph image.
I want the overwrite protection that is why I am using rename. Because after renaming I want to embed this image in PDF file.
Why not just use PHP to download the image?
download image file from given google chart api url using php
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="chart.png"');
$image = file_get_contents('http://chart.apis.google.com/chart? chs=300x300&cht=qr&chld=L|0&chl=http%253A%252F%252Fnetcane.com%252Fprojects%252Fyourl%252F3');
header('Content-Length: ' . strlen($image));
echo $image;