TCPDF outputs weird characters in IE8 - php

Today I started experimenting with PHP-based PDF generators. I tried TCPDF and it works fine for the most part, although it seems to be a little slow. But when I load the PHP file that generates my PDF in Internet Explorer 8, I see lines and lines of weird characters. Chrome however recognizes it as a PDF.
I'm assuming that I have to set a special MIME type to tell IE that it should interpret the page output as a PDF file. If yes, how can I do this?

putting "application/pdf" or "application/octet-stream" mime types might help. keep in mind that "application/octet-stream" will force download of the file and might prevent it from opening in the browser..
in case you wonder, you can do it like that:
header('Content-type: application/octet-stream');

I had this problem also but what I did to get it work is I added
exit();
at the end of pdf output.

You need to handle IE differently for dynamic-generated content. See this article,
http://support.microsoft.com/default.aspx?scid=kb;en-us;293792
In my code, I do this,
if(isset($_SERVER['HTTP_USER_AGENT']) AND ($_SERVER['HTTP_USER_AGENT']=='contype')) {
header('Content-Type: application/pdf');
exit;
}
This problem may also explain slowness you mentioned because your page actually sends the whole PDF multiple times without this logic.

#Pieter: I was experiencing the same issue using tcpdf (with fpdi), and loading the page that was generating the pdf using an ajax call. I changed the javascript to load the page using window.location instead, and the issue went away and the performance was much better. I believe that the other two posters are correct in the idea that the document header is causing the issue. In my case, because of the ajax call, the header was not being applied to the whole document, and causing the issue. Hope this helps.

I found this to be a problem too, and for me this all hinged on the code:
if (php_sapi_name( != 'cli') {
on line 7249 of the tcpdf.php file.
I commented this 'if' statement (and related '}')and all works fine for my other browser and ie8
Hope this helps

Related

PHP Media Link shows gibberish

Never done a lot of work with media files but I have an odd problem. I have a media link
http://.../wb_media/3343/64999/0aa2233675f94a4fc8a3915175e218f3/1/4e5b9927-3a46-4c69-9929-cc7e2a52f616.png
Which is suppose to show an image in the browser yet it shows gibberish:
Not sure where I should start looking to solve this? I have verified this is indeed the correct link. I would even appreciate knowing what that gibberish is called so I can research the problem more.
You must set header for the file type.
<?php
header("Content-type: image/png");
print (file_get_contents("location/to/image.png");
?>
Or if you are not printing it through php script, then you must look into server configuration. How server handles mime-types.

"download" attribute on <a> tag doesn't work on IE, safari or Opera

are there any standard way to make something similar?? I just want a way to download xml files from the server. Please help me!
No, as to my knowledge there is no way to do this using HTML.
You have to fix it on the target page. If a certain HTTP header is sent, the browser will offer a page for download instead of displaying it. This should work in every major browser. The necessary header is Content-Type: octet-stream. How you send this depends on your setup.
You can always send it by configuring your web server to do so, but how exacly depends on which web server you are using.
If, on the other hand, your XML file is generated by a PHP script, it's easy. Just add the following line before anything else is written, so preferably to the top of said script:
header('Content-Type: application/octet-stream');
If it's a static XML file... well, you could make a "proxy file" for that. Add a PHP file with the following content:
<?php
header('Content-Type: application/octet-stream');
// This "fakes" the file name, so the downloaded file isn't called
// "download_xml_file.php" or whatever you name the script.
header('Content-Disposition: attachment; filename=my_xml_file.xml');
readfile('path_to_the_actual_xml_file.xml');
?>
But try to avoid this hack. It's unnecessary bloat and it will break browser caching.
navigator.msSaveBlob(blob, filename)
https://msdn.microsoft.com/sv-se/library/windows/apps/hh772331
Unfortunately I don't know a way to do it in Safari.
Here you have a table with the browsers and thier compatibility with attribute download that Mike posted you in a comment: http://caniuse.com/download
And the actual tag with attribute is (just for sure you typing it right):
<a href="your_path_to_file" download>Download Me!</a>
-- it will work only in firefox, chrome and opera as it is in a table.
The download attribute only works in Firefox and Chrome. It will not work with IE, safari or Opera.

Web page garbled (encoding?) when downloading from within PHP

I am trying to download this page (http://www.360.ru/) from within PHP. However, when I write the file out and view it, the content is garbled/corrupt. However, a different page from the same site downloads with out problems (http://www.360.ru/goods/category/3/466/). And both work perfect well within Chrome & Firefox (which both report the encoding is UTF-8). I can not think what the problem can be. Here is my PHP code:
<?php
file_put_contents('/temp/out.html', fopen("http://www.360.ru/", 'r'));
file_put_contents('/temp/out2.html', fopen("http://www.360.ru/goods/category/3/466/", 'r'));
exit;
?>
When I open the two files, "out.html" is garbled, corrupt and "out2.html" is perfectly okay. Any help would be really appreciated. Thanks!
Ah, figured it out - the first page was gzipped. Using gzopen instead of fopen fixed the problem. Hope this helps others...

php/CSS formatting

We have changed hosting providers for our php/css website.
On the new providers server the formatting for the site looks a bit odd. ie the text is not correct and the titles are not in the correct place. All of the files have been uploaded including the CSS files.
Is there anything we should look for? maybe permissions? any help would be great never mind how simple.
Thanks
I'm getting multiple CSS errors off your "migrated" site in the Firefox error console: bad selectors, bad font names, etc.. But some of these errors don't occur on the old site.
In other words, your new site isn't identical to the old one.
The big one is that your /styles/stylesheet.css is actually a PHP file, with the raw PHP code being served up instead of CSS:
<?php
header("Content-type: text/css; charset=UTF-8");
$default = array(
'fontSize' => '75%'
);
The syntax errors are killing most of the CSS rules, which explains the differences.
Is the php version on the new server the same as on the old version?
I wouldn't expect minor differences in version to affect anything, but major differences in versions may cause unexpected behavior.
can you do a diff on the html source from the previous host and the new server. This assumes you still have access to the html on the old server.
Check all the files have been uploaded correctly (well all the css files) its possible there was an error and only a partial file was uploaded
View the HTML source and see what the referenced CSS files are. Try to load each one individually. If any fails, it means you have an access rights problem.

Displaying image with php

I have a script which displays images like this:
header("Content-Type: image/{$ext}");
readfile($image->path);
This has worked fine for weeks and now suddenly it has stopped working. The response header looks fine (Content-Type: image/jpg), I have no ending php-tag and I have made no changes to my code, server- or php-setup which could have caused this to malfunction. Does anyone have a clue as to what may be going wrong?
Thanks!
======================
UPDATE
The image doesn't display although you can download it (file->save as) and save it to computer. Openeing it locally though won't work either which leads me to think that the image has been corrupted somehow. Anyone experienced something similar? I'm thinking maybe som php errors/warnings get injected into the stream and corrupts the image.
One source of possible issues is that the MIME type for JPEG images is image/jpeg, not image/jpg. This is a case where the type doesn't agree with the fairly-common, 3-character version of the file extension.
Some thoughts:
File is to big
File path causing problems
The right content-type for JPG images is "Content-type: image/jpeg".
Note that the T of type is lower case.
UPDATE
I don't know if it will be useful but try something like this:
$info=pathinfo($image->path);
$ext=strtolower($info["extension"]);
if($ext=="jpg") $ext="jpeg";
header("Content-type: image/$ext");
imagejpeg(imagecreatefromjpeg($image->path));

Categories