2

The problem is that I'm POSTing some data to the PHP file, the post data are some parameters in from which the PDF will generate, I'd do it with an anchor or a normal form, but I'm posting complex data which JavaScript is helping me with.

I used console.log(data) on my success function of the JQuery .ajax method, and it prints the following.

HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Disposition: inline; filename="document.pdf" Content-Type: application/pdf Date: Fri, 03 Aug 2018 14:48:08 GMT %PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R /Font << /F1 8 0 R /F2 9 0 R >> /ExtGState << /GS1 10 0 R /GS2 11 0 R /GS3 12 0 R /GS4 13 0 R >> >> /MediaBox [0.000 0.000 595.280 841.890] >> endobj 4 0 obj [/PDF /Text ] endobj 5 0 obj << /Producer (�� d o m p d f) /CreationDate (D:20180803104808-04'00') /ModDate (D:20180803104808-04'00') >> endobj 6 0 obj << /Type /Page /MediaBox [0.000 0.000 595.280 841.890] /Parent 3 0 R /Contents 7 0 R >> endobj 7 0 obj << /Filter /FlateDecode /Length 673 >> stream ...lots of stream chars goes here... endstream endobj 8 0 obj << /Type /Font /Subtype /Type1 /Name /F1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >> endobj 9 0 obj << /Type /Font /Subtype /Type1 /Name /F2 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding >> endobj 10 0 obj << /Type /ExtGState /BM /Normal /ca 0.67 >> endobj 11 0 obj << /Type /ExtGState /BM /Normal /CA 0.67 >> endobj 12 0 obj << /Type /ExtGState /BM /Normal /ca 1 >> endobj 13 0 obj << /Type /ExtGState /BM /Normal /CA 1 >> endobj xref 0 14 0000000000 65535 f 0000000009 00000 n 0000000074 00000 n 0000000120 00000 n 0000000350 00000 n 0000000379 00000 n 0000000538 00000 n 0000000641 00000 n 0000001386 00000 n 0000001493 00000 n 0000001605 00000 n 0000001665 00000 n 0000001725 00000 n 0000001782 00000 n trailer << /Size 14 /Root 1 0 R /Info 5 0 R /ID[<f9dc912e3da657434ee0495c45e20e3a><f9dc912e3da657434ee0495c45e20e3a>] >> startxref 1839 %%EOF 

I'm sending the data to the client with a response, and I want to open a new tab-pop-up window with the PDF generated, but window.open() isn't doing the trick.

2
  • Opening new tabs windows is actively being fought by modern browsers so I am glad to see that this is still difficult. Your best bet is to use a modal window with an iFrame which links to the PDF file. Commented Aug 3, 2018 at 16:54
  • How could I actually do it? I searched on other questions, and collecting some answers I managed to get my stream output parsed to base64, and now when I open a new tab it just "pastes" the link to the pdf file, but I have to press enter or refresh the page so it actually loads, it's not a big deal but it's kinda annoying, makes you wonder if your connection is slow or not Commented Aug 3, 2018 at 20:12

1 Answer 1

1

As @MonkeyZeus mentioned, you have to call an iFrame to do the job because most browsers are blocking data:URL because of phishing and other issues (arrgh!), I tested opening the data:URL in Chromium and Mozilla Quantum, it worked, but Chrome won't load until you refresh the page (in my case, at least), so the code in the .ajax success status would be something like this:

... success:function(res){ var pdf= window.open("") pdf.document.write("<iframe width='100%' height='100%'"+ " src='data:application/pdf;base64, " + encodeURI(res)+"'></iframe>") } ... 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.