View PDF after AJAX call with TCPDF in Laravel 4

Asked

Viewed 297 times

2

After I save information in the database using the following code.

$this->anamnese->create($input);

I just call to test a code to generate a test PDF with the following code.

PDF::SetTitle('Hello World');
PDF::AddPage();
PDF::Write(0, 'Hello World');

$name = '/test.pdf';

$headers = array(
      'Content-Type: application/pdf',
    );

$filename = storage_path() . $name;

PDF::Output($filename);
return $filename;

And in the callback of the AJAX request I display a confirmation Alert.

However analyzing the element inspect, in the network tab, I get the following answer

%PDF-1.7
%����
7 0 obj
<< /Type /Page /Parent 1 0 R /LastModified (D:20141016181750-03'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 595.276000 841.890000] /CropBox [0.000000 0.000000 595.276000 841.890000] /BleedBox [0.000000 0.000000 595.276000 841.890000] /TrimBox [0.000000 0.000000 595.276000 841.890000] /ArtBox [0.000000 0.000000 595.276000 841.890000] /Contents 8 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Annots [ 6 0 R ] /PZ 1 >>
endobj
8 0 obj
<</Filter /FlateDecode /Length 295>> stream
x���Qo�0���+Σ��ۖ���9]|r[�-Q_&H�`�d  ٿ_%AQ�1p�
�m���ˁ�6�s�_X��+�9����`* �Sp[<9Fw��p?�#]vM���[FG�j�a�c��1)؋z�%���E�u�U�2
&4�L��,G�i�x/�<�������:Z��R�-�/�*-���p����QUU�g��lyQf��j��|7�����
�CI�I����z�C nt��u��*�Bm��V�k�ȚV9��л���~��_�*�҈����8�D�p;�� ���|�o
endstream
endobj

I have tried to put the headers and I can not display or force the PDF download, but testing without AJAX in a request to a route with only the code to generate the PDF, it generates and displays in the browser normally.

I would like after the insert in the bank to be generated a PDF and open in a new tab, with the information for printing.

Or another solution to generate PDF or print.

  • Have you tried in the sucess callback to get a different route that only download the pdf ?

  • So I thought about it, but for example, the file is in the Storage folder, as I would a route to it ?

  • It would look something like this, replacing the path of $file - http://stackoverflow.com/a/20415796/2099835

1 answer

2

In order for the browser to know that the content is to be downloaded, you must use the header Content-Disposition (English):

Content-Disposition: attachment; filename="meuFicheiro.pdf"

Additionally, according to the rules, the header should preferably be sent:

Content-Type: application/octet-stream

To basically indicate the browser:

I don’t know what this is, call the dialogue box Salvar como and uses if possible the name meuFicheiro.pdf.

I say above "preferably" because supposedly the browser while reading the stream PDF should identify the same as a PDF, although, due to the doubts, as the two headings give the complete indication that is a document to store on the user’s device.

Heading of identification of the document

You can also in the header Content-Type indicate that it is a PDF document:

Content-Type: application/pdf

Laravel

Alternatively, Laravel contains a method to indicate that the answer is a download:

return Response::download($caminhoParaFicheiro, $nomeFicheiro, $cabecalhos);

See the documentation on: Laravel: Special Responses (English)

  • I tried all this, and when I give the window.open in the ajax Success appears those codes still

Browser other questions tagged

You are not signed in. Login or sign up in order to post.