Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't open PDF on my iPhone #84

Closed
pboese opened this issue Aug 27, 2017 · 12 comments
Closed

Can't open PDF on my iPhone #84

pboese opened this issue Aug 27, 2017 · 12 comments

Comments

@pboese
Copy link

pboese commented Aug 27, 2017

Every time I try on my iPhone, I get an error message. I tried both send() and send('whatever.pdf'). None work. It works perfectly on Android and desktop browsers. Any Idea what
might cause this issue?

The script is here: https://www.die-partei-lichtenberg.de/mitgliedsantrag/

img_1691

Thanks in advance!

@mikehaertl
Copy link
Owner

mikehaertl commented Aug 28, 2017

Probably the same problem as described here?
https://stackoverflow.com/questions/20587520/php-file-download-script-doesnt-work-with-ios7-device

To verify you can try to comment out this line in mikehaertl/php-tmpfile:

https://github.com/mikehaertl/php-tmpfile/blob/master/src/File.php#L77

// header('Content-Length: '.filesize($this->_fileName));

@pboese
Copy link
Author

pboese commented Aug 28, 2017

Interesting, now I can see the PDF on my iPhone but none of the values are filled, it's just the blank form. You can try it here:

https://www.die-partei-lichtenberg.de/mitgliedsantrag/

At least the "Datum" (date) should be filled when submitting the empty form.

Same in Firefox and Chrome on my iPhone 7 :-(

@mikehaertl
Copy link
Owner

Things you could try:

If this doesn't work, I have no idea. Probably some idiosyncracy with PDF forms on iOS. PDF forms can be a real pain. But if you find out and we can implement a fix, let us know.

Regarding your initial problem: I wonder, if it makes sense to autodetect iOS devices and omit the Content-Length header in that case. What do you think?

@pboese
Copy link
Author

pboese commented Aug 28, 2017

I agree that it'd make sense to autodetect iOS, I implemented it this way:

if (!stripos($_SERVER['HTTP_USER_AGENT'], 'iPhone') && !stripos($_SERVER['HTTP_USER_AGENT'], 'iPad') && !stripos($_SERVER['HTTP_USER_AGENT'], 'iPod')) {
    header('Content-Length: '.filesize($this->_fileName));
}

Also tried this one: $pdf->fillForm($data, 'UTF-8', true, 'FdfFile') but it still doesn't work. Not on my iPad nor on my iPhone :-( Damn Apple!

I really don't know too much about filling PDF forms and don't know where to start debugging so my workaround would be to offer the blank PDF to iOS users and tell them to fill the PDF themselves ;-)

@mikehaertl
Copy link
Owner

@pboese I've pushed a slightly modified version of your fix to master on https://github.com/mikehaertl/php-tmpfile. Could you maybe help testing? You only have to replace the File.php in mikehaertl/php-tmpfile with the version here: https://github.com/mikehaertl/php-tmpfile/blob/master/src/File.php

@mikehaertl
Copy link
Owner

I really don't know too much about filling PDF forms

Don't worry, me neither 😆

and don't know where to start debugging so my workaround would be to offer the blank PDF to iOS users and tell them to fill the PDF themselves ;-)

Sounds like a solid workaround. :)

@mikehaertl
Copy link
Owner

Also tried this one: $pdf->fillForm($data, 'UTF-8', true, 'FdfFile') but it still doesn't work. Not on my iPad nor on my iPhone :-( Damn Apple!

One more note: it should be fdf, not FdfFile.

@pboese
Copy link
Author

pboese commented Aug 28, 2017

Solid indeed :D shame on you, Apple!

Your fix works as expected. I used stripos() because I wasn't sure if preg_match() wasn't a bit slower, but it probably won't matter too much in this case anyways.

It doesn't matter if it's fdf or FdfFile as long as it's not xfdf

$className = '\mikehaertl\pdftk\\'. ($format==='xfdf' ? 'XfdfFile' : 'FdfFile');

But sounds more logic, yeah :-)

@mikehaertl
Copy link
Owner

At least it's a known problem:

https://forums.adobe.com/thread/2266388
https://discussions.apple.com/thread/3118042?tstart=0

Sorry we couldn't fully fix your problem. But thanks for your help. This eventually helped to fix a download issue with iOS. I'll create a new release for php-tmpfile now, containing your fix.

@krukru
Copy link

krukru commented Sep 10, 2019

Downloads using HTTP/2 protocol do not work for the same (or similar) reason. When there is content-length set and using HTTP/2 files from the browser fail to download (even using curl).
Is there any way to disable content-length for HTTP/2 requests?

@mikehaertl
Copy link
Owner

@krukru I don't want to add too many workarounds especially if it's not clear why it's failing. We do nothing wrong IMO.

As a workaround I'd suggest you write your own download logic. Something like this should work:

// Instead of send() or saveAs():
$pdf->execute();
$filename = $pdf->getTmpfile()->getFileName();

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
readfile($this->_fileName);

@mikehaertl
Copy link
Owner

@krukru and others: I'm still struggling to find a solution for this. The problem is that you can't reliably detect HTTP/2 from PHP (at least not if you consider proxy setups). If anyone has an idea feel free to comment here: mikehaertl/php-tmpfile#16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants