I am generating a pdf using pdftk into an iframe with:
<iframe src="data:application/pdf, <?php passthru('pdftk example.pdf output -'); ?>"> </iframe>
However the browser (chrome) reports:
"The file was damaged and could not be repaired"
What am I doing wrong?
If I have a separate file, and reference that within the iframe it works fine. the separate filecontent is:
<?php
header("Content-type: application/pdf");
passthru('pdftk example.pdf output -');
?>
According to http://us1.php.net/manual/en/function.passthru.php, the passthru function is intended to return binary data. I'm not sure if there's enough room in the src attribute to fit all that binary data.
Take that PHP snippet, save as a something.php file, and use that as the src="something.php" - that should work.
Related
I want to display, embedded in the HTML, a pdf file which is in a directory up to the document root.
Document root points to /var/www/web1/web and the pdf file is in /var//www/web1/docs/pdf, so I can't point it from the html.
I'm using PHP and I tried some suggestions like get_file_contents, base64_encode, .... but didn't worked.
All examples I find are to make download the pdf or to convert it into a jpg, but this is not what I need.
Any suggestions?
Simply load the pdf content by a php page, we call it viewer.php, then embed the PHP page as it would be the document itself:
<embed src="viewer.php" width="80%" height="900px" />
In the viewer.php file:
<?php
//Load file content
$pdf_content = file_get_contents('../unreachable_file_outside_webserver.pdf');
//Specify that the content has PDF Mime Type
header("Content-Type: application/pdf");
//Display it
echo $pdf_content;
Otherwise another solution for bigger files (easier on RAM due to bufferized read/output without storing content in RAM) use readfile
<?php
header("Content-Type: application/pdf");
readfile("../unreachable_file_outside_webserver.pdf");
You can embed PDF on your website using a library, for example there's a great one called PDF.js
PDF is a binary format so you cannot exactly "display it".
Use the following html tag:-
<embed src="path_to_pdf_file.pdf" width="800px" height="2100px" />
You first need to go to root dir using "../"
<embed src="../docs/pdf/fileName.pdf" type="application/pdf" width="100%" height="500px" />
I uploaded the .doc file using php code and saved it in a folder ,the path is stored in database.
When i tried to make a view of the doc file in a div ...a dialog box appears asking whether to save or openwith ..
I'm using wampserver ....I just tried like this
<iframe name="awindow" frameborder=2 width=580 height=440 src="www/siva_example/pdf/1_siva.doc"></iframe>
Any help regarding..
Is it possible to view .doc file in browser or i have to convert it to pdf format...
It's not possible to display a .doc[x] in HTML. You can try to convert the .doc[x] to HTML or to an image.
you can view doc file by adding this to your header at request time
<?php
header('Content-disposition: inline');
header('Content-type: application/msword');
// not sure if this is the correct MIME type
readfile('MyWordDocument.doc');
exit;
You could try using google docs for this, use the below code, it works perfectly for ppts, have not been able to test it for a doc yet
<iframe src="http://docs.google.com/gview?url=http://yourdomain.com/document.doc&embedded=true" style="width:550px; height:450px;" frameborder="0"></iframe>
Ofcourse you would have to update the url as per your program using PHP
I need to display a (pdf,doc,docx,txt) documents inside an php web form with the ability to select a part of the document content and do some processing on it in the right click (like saving the selected content to DB)?
I try this way :-
<iframe src="http://docs.google.com/viewer?url=<?=urlencode($docx)?>&embedded=true" width="600" height="780" style="border: none;"></iframe>
But is show me as html
PHP can set the header information for a file and so you can display anything as file.php, loading the file.php you can then specify what it should be handled as- for example you can generate an image and output it as a jpeg file :
header('Content-Type: image/jpeg');
$img = LoadJpeg('bogus.image');
imagejpeg($img);
imagedestroy($img);
from http://php.net/manual/en/function.imagecreatefromjpeg.php example.
Using a similar method for setting the header content-type you can output PDF contents or DOC or whatever, by setting the header to the correct file type and loading the contents of the file with PHPs file_get_contents() ( http://php.net/manual/en/function.file-get-contents.php ) function, and outputting that to the browser / iframe.
Two things you can do:
Just link to the .pdf/etc. location on your server
Save the image of it as a .jpg and display that then put a link to download the pdf/whatever
I found this topic: convert text to image in php
and I tested the code that Sourabh Shankar provide.
When I paste this code in a new empty php file and I adjust the font path, then it works perfect!
But when I put a echo before or after the header('Content-type: image/png'); line, then it all fails
Can someone provide me a code of how to call this image creator in a HTML file:
example:
<span>Here comes the image</span>
<?php [call function??] ?>
<span>and here we continue with some HTML</span>
Do I need to create a php function and if so:
what do I excactly return?
if I return this:
imagepng($im);
then he wont execute the
imagedestroy($im);
I'm not sure how to do this
The PHP code you linked to works as a stand-alone file, so that when it is requested in a browser it will send back an image. Setting the header with
header('Content-type: image/png');
means that the code will return content which the browser interprets as an image file, rather than a HTML file.
You couldn't use PHP to directly output a file into HTML. You would need to use that code in a separate file and link it to the src tag of an image in your code.
You need get image from other script via http.
<span>Here comes the image</span>
<img src="/path/to/image_handler.php"/>
<span>and here we continue with some HTML</span>
where image_handler.php - your php script:
<?php
…
header('Content-type: image/png');
imagepng($im);
NOTE: never output something before call header
After using the PHP QrCode lib I discovered that for some reason when using a dynamic page with scripts and dialog boxes (JQuery) that when trying to output a QR code in a .png format I get weird symbols instead of the actual generated .png file.
Heres what I have tried:
Created a seperate file with just:
<?php
//include only that one, rest required files will be included from it
include "phpqrcode/qrlib.php";
QRcode::png('barrda554');
?>
Works great,
Attempt 2:
File opened within a dialog box type using JQuery UI:
<?php
header stuff...
include "phpqrcode/qrlib.php";
...
?>
<html>
...
<?php
QRcode::png('barrda554');
?>
..
</html>
In this attempt I get multiple funky symbols for some reason:
�PNG IHDRWWKK/PLTE���U��~�IDAT8��ѱ � P# �c��n :V�L�#�k
y��)�|F��5`ڸzHF|l���
%Z"e�Ы�D{\�ގ����p`�f�eh�������k�[BJeJ�c����,�^�gu�m|Q��o��W����g�
�#�s�<�y��k�m��!v�.��(+�u���$s�-�n$߫>�gR�`IEND�B`�
This has stumped me and I am unsure on how I should approach this to fix it.
Let me know your ideas,
David
UPDATE:
After putting header('Content-Type: image/png'); within the file that JQuery opens, no cigar.
Here is the actual file:
http://jsfiddle.net/T4nEP/
The problem is here:
<html>
<?php
QRcode::png('barrda554');
?>
</html>
To understand what this is doing, imagine that you open a regular PNG file in a text editor, and just copy/paste the contents directly into your HTML file. It's not going to show an image - it'll just be garbage, like you're seeing.
To include an image in an HTML file, you need to use the <img> tag, and point to the URL of the image. In this case, the URL of the image would be a PHP script that outputs nothing except the PNG contents - like this:
<img src="qrcode.php">
And then in qrcode.php, generate the image:
<?php
include "phpqrcode/qrlib.php";
QRcode::png('barrda554');
?>
If you need some information from the HTML page in order to generate the image, you can include it as query parameters in the URL, like this:
<img src="qrcode.php?product=1&format=2">
And then get those values in your PHP like this:
<?php
include "phpqrcode/qrlib.php";
$product = $_GET['product'];
$format = $_GET['format']
// ...
// whatever you need to do to generate the proper code
// ...
QRcode::png('barrda554');
?>
And finally - there are ways to include the image data directly into the HTML page, but it's not supported by all browsers, and is not recommended because it makes the page size much larger and prevents the browser from being able to cache the image separately.
You can see more about base64 data URLs here and here.
Make sure you have the following php code to generate the correct Content-Type header so the browser knows how to render the data its receiving.
header("Content-Type: image/png");