I want to read content from a text file and download it into a pdf file using PHP object orientation. How I read content from a file, is it same as simple PHP?
The simplest way to get a file’s contents is file_get_contents:
$contents = file_get_contents('filename');
Do you want to know how to get the contents of a file using object-oriented notation, or how to store the contents as an object, or how to move the contents into a pdf?
Assuming the last 2:
//First set the file path and get the contents of the file:
$textfile->path = "path/to/file.txt";
$textfile->contents = file_get_contents($textfile->path);
//Next create the pdf, both as a handler and as a file on disk:
$pdf = PDF_new();
PDF_begin_document($pdf, "file.pdf", "");
//Then put the text file contents into the pdf:
PDF_show($pdf, $textfile->contents);
//Finally, save and close the pdf:
pdf_save($pdf);
pdf_close($pdf);
If you want the script to return the pdf from a request and not save it to the server, simply change "file.pdf" to "" and use the header() function to set the filename.
Related
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'm using mPDF library to provide pdf files.
Assume that I have a file named facture.php that contains :
an image in the header
a table in the body
an image in the footer of the file
And another file named convert_HTML2PDF.php that will provide the pdf output of the first file.
I would like to get the output of the facture file that is interpreted into a variable and output it as a pdf file without visualising in the browser.
How can I do that and use the output inside the convert_HTML2PDF.php?
$pdf = $mpdf->Output('', \Mpdf\Output\Destination::STRING_RETURN)
will output the PDF into a string - no need to create temp file.
Use file_get_contents() to get html content of your facture.php :
$output = file_get_contents('http://www.example.com/facture.php');
$mpdf->Output('filename.pdf', 'F');
will send the output to a file on your server instead of a browser.
See:
http://mpdf1.com/manual/index.php?tid=125
For more output variations.
I am writing a simple PHP file to upload a file on POST, and to also create a new image and include the path to that new image in the response.
For the JSON response I have set headers in the main PHP file that the POST is sent to - like so:
header("Content-Type: application/json");
Just before my JSON encoded echo this is.
Above that part I call a function from another PHP file that uploads the POST file, and one that is called after that rendering a different image using that uploaded file.
In that function in order to create the PNG file I need put on the disc I have to declare a new header right? Like so:
header("Content-type: image/png");
I then ofc get the doublet header problem.
How can I get around this problem?
I cannot use header_remove() BTW.
"In that function in order to create the PNG file I need put on the
disc I have to declare a new header right?"
No.
Headers should only be sent to browsers. You don't need to send a header to write to disc.
In that function in order to create the PNG file I need put on the
disc I have to declare a new header right?
You don't have to declare a new header. As Danack pointed out, headers are only used for sending information to the browser. For instance, if you want to output the binary data of that image and then call that header, it will render the image in the browser. For writing the image to disk, it's fairly simple.
<?php
move_uploaded_file($_FILES['image']['tmp_name'], 'Path/To/File/Location/'.$_FILES['image']['name']);
?>
And that should do it.
I have moved my images to Rackspace Cloud Files and am using their PHP API. I am trying to do the following:
Get an image from my "originals" container
Resize it, sharpen it, etc.
Save the resized image to the "thumbs" container
My problem is with #2. I was hoping to resize without having to copy the original to my server first (since the images are large and I'd like to resize dynamically), but can't figure out how. This is what I have so far (not much):
$container = $conn->get_container("originals");
$obj = $container->get_object("example.jpg");
$img = $obj->read();
Part of the problem is I don't fully understand what is being returned by the read() function. I know $img contains the object's "data" (which I was able to print out as gibberish), but it is neither a file nor a url nor an image resource, so I don't know how to deal with it. Is it possible to convert $img into an image resource somehow? I tried imagecreatefromjpeg($img) but that didn't work.
Thanks!
First, you cannot resize an image without loading it into memory. Unless the remote server offers some "resize my image for me, here are the parameters" API, you have to load the image in your script to manipulate it. So you'll have to copy the file from the CloudFiles container to your server, manipulate it, then send it back into storage.
The data you receive from $obj->read() is the image data. That is the file. It doesn't have a file name and it's not saved on the hard disk, but it is the entire file. To load this into gd to manipulate it, you can use imagecreatefromstring. That's analogous to using, for example, imagecreatefrompng, only that imagecreatefrompng wants to read a file from the file system by itself, while imagecreatefromstring just accepts the data that you have already loaded into memory.
You can try to dump the content of the $img variable into a writable file as per the below:
<?php
$filename = 'modifiedImage.jpg';
/*
* 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate
* the file to zero length. If the file does not exist, attempt to create it.
*/
$handle = fopen($filename, 'w+');
// Write $img to the opened\created file.
if (fwrite($handle, $img) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote to file ($filename)";
fclose($handle);
?>
More details:
http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.fwrite.php
Edit:
You might also want to double check the type of data returned by the read() function, because if the data is not a jpg image, if it's for example a png, the extension of the file needs to be changed accordingly.
I created xml file in php. This file is successfully saved in my server directory.
Now, I want to download this file only through php, not javascript.
How can I save this file?
Try file_get_contents() and file_put_contents().
<?PHP
$xml = file_get_contents("http://yoursite.com/yourxml.xml"); // your file is in the string "$xml" now.
file_put_contents("/path/to/file/yourxml.xml", $xml); // now your xml file is saved.
?>