2

maybe you can help me. When I try to show a pdf I created I got some weird symbols like this

u\>�'���O���r>c!%�@�R�`YPd+��vv����1��E�'^k-�WD�*+��W^��wy��V Z��dUdJ�B���C�ڳtK����j:c���5����50���D3lgH#�}%���D+������ix����,��-�'\�� �_st^&0�Y���������v�*Ӗ,W����u!H��sNN��0cӝ��`xEk��d��^� �8K9�BL����9�̋"6/�E�|�̛�-�7�P��B�#�T�F���4`����

What I do is transform a html file with this code

    $html = $this->load->view('ReporteIngresoView', $data, TRUE);
    $pdf = new Pdf('P', 'mm', 'A4', TRUE, 'UTF-8', FALSE);
    $pdf->SetTitle('Reporte ingreso');
    $pdf->SetHeaderMargin(30);
    $pdf->SetTopMargin(40);
    $pdf->setFooterMargin(20);
    $pdf->SetAutoPageBreak(TRUE);
    $pdf->SetAuthor('Pontificia Universidad Católica del Ecuador');
    $pdf->SetDisplayMode('real', 'default');
    $pdf->AddPage();
    $pdf->writeHTML($html, TRUE, 0, TRUE, 0);
    $pdf->Output('My-File-Name.pdf', 'I');  

On the other side I recieve it like this

Ext.Ajax.request
            (
                {
                    method: 'post',
                    url: '../servidor/archivo/ingreso/getreporte',
                    success: function(response){
                    Ext.getCmp('winReporteRegistro').update( response.responseText );
                       }
                    }
            );

I am using php (server), html (create the page), extjs (interface), ajax (request) and TCPDF to create the pdf.

Any ideas?

Thanks in advance,

  • Maybe its encoding issues? Check this to find convertion by "weird symbols" https://habrastorage.org/storage2/61b/e7a/455/61be7a4552a6650bd19b526db63d4779.png Its in russian, but I think you can easy understand it. – Sergey Novikov Sep 29 '17 at 19:50

3 Answers3

0

You need to convert the response to a Blob (link)

After creating the blob, you can create an URL object (link)

This is how I create a server side generated pdf and display it on the front end.

Adding code snippet:

var blob = new Blob(response.responseText, {type: 'application/pdf'}); 
var objectURL = URL.createObjectURL(blob);
veggirice
  • 246
  • 1
  • 2
  • 15
  • Something like this? var blob = new Blob(response.responseText); objectURL = URL.createObjectURL(blob); Ext.getCmp('winReporteRegistro').update(objectURL); The window is not showhing anything :( –  Sep 29 '17 at 19:03
  • 1
    close to what i have. var blob = new Blob(response.responseText, {type: 'application/pdf'}); var objectURL = URL.createObjectURL(blob); Once you have an object URL, you can show it as a download link or download the pdf directly. Also found this [link](https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link) – veggirice Sep 29 '17 at 19:19
  • I tried to add a window var window = Ext.create('Ext.window.Window', { items: [{ xtype: 'component', autoEl: { tag: 'a', href: objectURL, html: 'Example.com' } To show the hyperlink but still no luck. The window doesn't appear –  Sep 29 '17 at 19:42
  • 1
    can you just do a simple `window.open(objectURL);` It should open it in a new browser window – veggirice Sep 29 '17 at 19:53
  • It doesn't work, nothing happens. Ext.Ajax.request ( {method: 'post',url: '../servidor/archivo/ingreso/getreporte',success: function(response){ var blob = new Blob(response.responseText, {type: 'application/pdf'}); var objectURL = URL.createObjectURL(blob); window.open(objectURL); }, } ); I don't know why.. –  Sep 29 '17 at 19:59
  • can you debug and see what is being returned in your success response? could be something to do with the response and creating the blob? – veggirice Sep 29 '17 at 20:42
  • Yes, I used $pathservidor = 'c:/wamp64/www/Archivo/pdf/'; $pdf->Output($pathservidor . uniqid() . '.pdf', 'F'); and it creates an empty pdf file in that directory –  Sep 29 '17 at 20:56
0

Returning the location of the file and open that in another web page was the answer, the code is:

Server:

$html = $this->load->view('ReporteCajaView', $data, TRUE);

    $pdf = new Pdf('P', 'mm', 'A4', TRUE, 'UTF-8', FALSE);
    $pdf->SetTitle('Reporte item');
    $pdf->SetHeaderMargin(30);
    $pdf->SetTopMargin(20);
    $pdf->setFooterMargin(20);
    $pdf->SetMargins(25, 20, 20, true);
    $pdf->SetAutoPageBreak(TRUE);
    $pdf->SetAuthor('Universidad Católica');
    $pdf->SetDisplayMode('real', 'default');
    $pdf->setPrintHeader(FALSE);
    $pdf->setPrintFooter(FALSE);
    $pdf->AddPage();

    $id = uniqid();
    $pdf->writeHTML($html, TRUE, 0, TRUE, 0);
    $pathservidor = 'c:/wamp64/www/Archivo/pdf/';
    $pdf->Output($pathservidor . 'Reporte de caja' . '.pdf', 'F');

    echo 'c:/wamp64/www/Archivo/pdf/'.'Reporte de caja'.'.pdf';

Client:

Ext.Ajax.request
            (
                {
                    method: 'post',
                    url: '../servidor/archivo/item/getreporte',

                    success: function(response){
                    window.open(response.responseText);

                       }
                }
            );

Thanks!

0

Sorry for the late answer but I broke my teeth on this yesterday and this morning I had an illumination :-) You need to support the mime-type so simply add this in your .htaccess:

AddType application/pdf .pdf