Send function not working properly #97

Closed
carlotrimarchi opened this Issue Apr 30, 2015 · 22 comments

Projects

None yet

4 participants

@carlotrimarchi

Hi,
I'm trying to generate pdf files in Codeigniter.

I set the library like this:

    $this->load->library('mypdf');
    $this->pdf = new Pdf;
    $options = array(
        'binary' => '/usr/local/bin/wkhtmltopdf'
    );
    $this->pdf->setOptions($options);
    $content = $this->load->view('pdf/test_new_pdf', '', true);
    $this->pdf->addPage($content);
    $this->pdf->send();
    $this->pdf->saveAs('./public/pdf/payment_requests/test.pdf');

I'm using the example code you posted in the documentation as content for the pdf, but when I try to generate a pdf on the browser the error message "Failed to load PDF document" appears. Both on Firefox and Chrome.
The pdf file, though, is generated.

I've found out that to make the "send" command to work I have to remove all the html tags between body, like this:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

        This is an example header.
        Demo
        This is example content
        This is an example footer.

</body>
</html>

Any suggestions?
Thanks!

@carlotrimarchi

An update:

If I remove the <h1> tags from the html the send() function is actually working. It seems to be working fine with div and <p> tags. It also doesn't work when I try to add a font-family to the css.

@mikehaertl
Owner

There's not really any restriction on the HTML you can use. So either way, it should work. Have you checked for an error?

if (!$pdf->send()) {
    throw new Exception('Could not create PDF: '.$pdf->getError());
}

Also if the file is sent, what is the content of the file? Can you send it for download ($pdf->send('test.pdf')) and see what the file contains? Sometimes this helps to find the error message.

@carlotrimarchi

Hey,
thanks for the answer.

Here's a generated pdf:

On Thu, Apr 30, 2015 at 12:56 PM, Michael Härtl notifications@github.com
wrote:

There's not really any restriction on the HTML you can use. So either way,
it should work. Have you checked for an error?

if (!$pdf->send()) {
throw new Exception('Could not create PDF: '.$pdf->getError());
}

Also if the file is sent, what is the content of the file? Can you send it
for download ($pdf->send('test.pdf')) and see what the file contains?
Sometimes this helps to find the error message.


Reply to this email directly or view it on GitHub
#97 (comment)
.

@mikehaertl
Owner

I don't see any PDF file. Also, please first try for yourself if you can open it e.g. in a text editor. I'm not here to do the debugging for you ;)

@carlotrimarchi

Sorry, I replied by email attaching the file and it doesn't work on github. I can only attacch images, so here's a link to the pdf:

https://dl.dropboxusercontent.com/u/189424/test2.pdf

I opened it with an editor but I couldn't understand what's wrong

@mikehaertl
Owner

If i open this document, the first line contains the wkhtmltopdf command that was used to create the PDF. This line should not be there.

/usr/local/bin/wkhtmltopdf '/private/var/tmp/tmp_wkhtmlto_pdf_xxS2mv.html' '/private/var/tmp/tmp_wkhtmlto_pdf_PTNWf8.pdf'%PDF-1.4
1 0 obj
<<
/Title (��)
/Creator (��wkhtmltopdf 0.12.2.1)
/Producer (��Qt 4.8.6)
/CreationDate (D:20150430142646+02'00')
>>
endobj
3 0 obj

So you must echo this out somewhere from your PHP script.

@carlotrimarchi

Well, I figured out that the problem was with a webfont failing to load.

Still, other weird stuff keeps happening. You're right saying that that line should not be there, but that line is somehow added when the send command fails. For example, now I noticed that this character &quot; is causing the error. The pdf gets created correctly with the saveAs command (opening with the text editor I don't see that line), but if I use send it's the same issue again. And it is the same exact code.

@mikehaertl
Owner

If the generation fails, then what do you expect? It will always send some erroneous file in this case. You should first make sure, that the command does not fail.

I'm using the send() method, too and it works fine for me.

@carlotrimarchi

The generation doesn't fail.
The ´saveAs()method outputs a perfectly working pdf file. Thesend()` method, on the other hand, outputs a corrupted pdf which I can't even open.

@mikehaertl
Owner

Still I can not see, how this should happen. Do you combine both in one request? If you do: What if you just use send() without saveAs()?

@mikehaertl
Owner

Quoting a comment by @zealfire:

I tried to debug and I am facing this warning:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\project\core\vendor\mikehaertl\php-tmpfile\src\File.php:77) in mikehaertl\tmp\File->send() (line 69 of C:\xampp\htdocs\project\core\vendor\mikehaertl\php-tmpfile\src\File.php)

Line 77 says this: readfile($this->_fileName);

Update: I modified the line where it says: header('Content-Length: '.filesize($this->_fileName)); and made it as comments and my problem got solved. Can you explain why?Is this problem faced by me only or it has happened previously too. According to me $this->_fileName is outputting the content beforehand which is causing a problem.

@mikehaertl
Owner

@zealfire: This code is actually an exact copy of an example from the PHP manual, see here:

http://php.net/manual/en/function.readfile.php

It's weird that it works without. I remember that Apache adds this header back in automatically in some situations. Maybe you can verify this in your browser? Try once with and without that line and see, if the Content-Length header is still there.

UPDATE:
Could be related: http://stackoverflow.com/questions/1972642/sending-correct-file-size-with-php-download-script

@zealfire
zealfire commented Jun 4, 2015

I previously forgot to tell you that though pdf is rendering inline but the warning has still not suppressed.Also I don't see any Content-Length header when I have removed it(obvious) and when I am added that line in the code I face this message "Failed to load PDF document".Can u suggest any ways to remove the warning .Btw when I am using these lines:

if (!$this->pdfGenerator->send()) {
throw new Exception('Could not create PDF: '.$this->pdfGenerator->getError());
}
warning is being generated.Thanks.

@mikehaertl
Owner

@zealfire Just to verify that you really have the same issue as @carlotrimarchi : Does saveAs() work for you?

@zealfire
zealfire commented Jun 4, 2015

Yes saveAs() works for me too.

@mikehaertl
Owner

@zealfire I've removed your comment as it was not related to this issue. Please open a new issue if you think there's something wrong.

@mikehaertl
Owner

I've just undone a change in https://github.com/mikehaertl/php-shellcommand, which was reported in #111 to cause a similar issue.

So could you please try to update mikehaertl/php-shellcommand? Latest version is 1.0.6:

composer update mikehaertl/php-shellcommand
@zealfire

@mikehaertl thanks for looking into issue for me previous error message has gone but I am now facing this problem:

Loading pages (1/6) [> ] 0% [======> ] 10% [========> ] 14% [=========> ] 16% [==================> ] 30% [============================================================] 100% Counting pages (2/6) [============================================================] Object 1 of 1 Resolving links (4/6) [============================================================] Object 1 of 1 Loading headers and footers (5/6) Printing pages (6/6) [> ] Preparing [============================================================] Page 1 of 1 Done Exit with code 1 due to network error: ContentOperationNotPermittedError

@zealfire

@mikehaertl On further investigation I came to conclusion that inclusion of base tag in head section of html was cause of problem. In the head section I was writing a code like this:

<base href=" \path\to\file" \>

which was causing Exit with code 1 due to network error: ContentOperationNotPermittedError but after removing it the error was removed. Any reasons for this?

@mikehaertl
Owner

@carlotrimarchi Please let me know if the send() problem is still relevant for you.

@zealfire I have no idea and the problem is probably more on the side of wkhtmltopdf, not my library.

@mikehaertl
Owner

As there was no further feedback and I could not reproduce the issue, I'm closing this now.

@mikehaertl mikehaertl closed this Aug 10, 2015
@rahulmangal1990

Hello when i genrate pdf using wkhtmltopdf some time working well. but sometime

while (true)
{
var read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);

                    if (read <= 0)
                    {
                        break;
                    }
                    ms.Write(buffer, 0, read);
                }

p.StandardOutput.BaseStream.Read not reading html from html file. give some error like task was cancelled..?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment