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

Problem to output PDF in Symfony 4 #399

Open
Syulwen opened this Issue Oct 25, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@Syulwen

Syulwen commented Oct 25, 2018

Hi !
I'm trying to generate a PDF from complex HTML and the rendered PDF is totally blank. I also tried with the example00, same result.

Here's my code :

Controller :

/**
     * @Route("/customTDB/generate", name="customtdb_generate")
     * @param $request Request
     * @return Response
     */
    public function generate(Request $request)
    {
        $customTDB = new CustomTDBUtils();
        $pdf = $customTDB->generateTestPDF();
        $response = new Response($pdf);
        $response->headers->set('Content-Type', 'application/pdf');
        return $response;
    }

Class CustomTDB :

public function generateTestPDF()
    {
        ob_start();
        include 'testpdf.html';
        $content = ob_get_clean();

        $html2pdf = new Html2Pdf('P', 'A4', 'fr');
        $html2pdf->setDefaultFont('Arial');
        $html2pdf->writeHTML($content);
        return $html2pdf->output('example00.pdf', 'D');
    }

The request is send by AJAX, that way :

$.ajax({
            url: '/customTDB/generate',
            type: "POST",
            success: function (response, status, xhr) {
                var filename = "my-tdb.pdf";
                var disposition = xhr.getResponseHeader('Content-Disposition');
                if (disposition && disposition.indexOf('attachment') !== -1) {
                    var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                    var matches = filenameRegex.exec(disposition);
                    if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                }

                var type = xhr.getResponseHeader('Content-Type');
                var blob = new Blob([response], {type: type});

                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
                    window.navigator.msSaveBlob(blob, filename);
                } else {
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);

                    if (filename) {
                        // use HTML5 a[download] attribute to specify filename
                        var a = document.createElement("a");
                        // safari doesn't support this yet
                        if (typeof a.download === 'undefined') {
                            window.location = downloadUrl;
                        } else {
                            a.href = downloadUrl;
                            a.download = filename;
                            a.target = '_blank';
                            document.body.appendChild(a);
                            a.click();
                        }
                    } else {
                        window.location = downloadUrl;
                    }

                    setTimeout(function () {
                        URL.revokeObjectURL(downloadUrl);
                    }, 100); // cleanup
                }
            }
        });

testpdf.html is a copy/paste of example00

I've tired a loooot of things without any result, so if you had an idea, I'll be glad to read it ^^"

@spipu

This comment has been minimized.

Owner

spipu commented Oct 26, 2018

if you have a blank page, look at the apache and php log :)

@spipu spipu added the help wanted label Oct 26, 2018

@th3squal

This comment has been minimized.

th3squal commented Nov 19, 2018

You don't need tu use buffer with html page, juste juse file_get_contents.
And if you need dynamic page, use twig instead.

Debug will be easier.

Try this kind of code with html file, it works fine for me

        $content = file_get_contents('testpdf.html');
        $html2pdf = new Html2Pdf('P', 'A4', 'fr');
        $html2pdf->setDefaultFont('Arial');
        $html2pdf->writeHTML($content);

        return new Response(
                    $html2pdf->Output( 'example00.pdf', 'D'),
                    200,
                    array(
                        'Content-Type' => 'application/pdf',
                        'Content-Disposition' => 'attachment; filename="example00.pdf"',
                    )
                );

if you use twig, create a template, use twig service's createTemplate method, and render the template with your data.

//Data you want to pass to template
$data=[];
//TEMPLATE FROM STRING
$twigTemplate = $this->get('twig')->createTemplate($stringContainingTwigTemplate);
$html = $twigTemplate->render($data);
//TEMPLATE FROM TWIG FILE
$html = $this->render('path/to/template.html.twig', $data);

if your problem is still here, you've got probably a wrong html format (unclosed tag for exemple).

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