Laravel snappy pushing PDF download - javascript

I am using Laravel/Jquery with barryvdh/laravel-snappy to create a pdf on the fly from database data.
In the controller I have:
if ($request->ajax())
{
$document = Document::url($id)->first();
$pdf = PDF::loadView('app.documents.whitepapers.pdf', compact('document'));
return $pdf->download('filename.pdf');
// return response()->send($pdf->download('filename.pdf'), 200, $headers);
}
How can I push this PDF to the browser so that is available as a download?
When I do it without Jquery it is working perfectly. The reason that it is behind a Jquery function is that I use a Modal with a form to collect a persons data before the download can be initiated. This has ajax validation.

You will not be able to present the file as a regular download via JavaScript. In order to show a download prompt to the user, you'll need an extra step in your workflow.
I would suggest the following workflow:
User submits modal form
Form is validated via AJAX. If validation passes, respond with a URL to download the user's PDF from, instead of the PDF itself.
Your JavaScript redirects the user to this URL. Note that this is a redirect, rather than another AJAX request (see top.location.href)
When the user accesses that URL, you are able to deliver the PDF using the code snippet you provided in your original question. This snippet sets the content-disposition of the response appropriately, forcing the download prompt to appear in the user's browser.
If you need to customise the PDF generation for each user, make sure to include the user's ID (or another piece of identifying information) through to step 4.

Related

Set cookie when sending request using cURL

There is an external website A (not mine) which provides a PDF document after filling an HTML form.
Here is the scenario when I do the procedure on site A :
I open my Internet browser on the site A homepage (I will call this
page A1), then a JESSIONID is created into my browser's cookie
I fill the form on a page A2
if the form is correctly filled, i am redirected to a page A3,
from which a link is available to download a PDF file, using url :
A.com/getPdf.jsp
I now want to allow users to dowload this PDF file from my website B, using my own form.
To do so :
step 1 : I am sending the form inputs to website A using PHP cURL.
This step is working successfully, and I got a JSESSIONID reference.
step 2 : then I am opening a window from my website using URL :
A.com/getPdf.jsp
But it does not provide me the needed document as the session state is not maintained between previous steps 1 and 2...
So my question is to know how can I call A.com/getPdf.jsp URL, using the same state (same JESSIONID) than the one I retrieved just before from the cURL request?
Thanks in advance for your suggestions.

Acrobat Javascript: passing a UTM parameter from URL into a PDF button

I have a PDF that has a button with field name ctaButton.
ctaButton currently has a url pointing to https://mywebsite.com.
I want to host the PDF on my server at https://mywebsite.com/hosted.pdf.
And when I send someone a link to the PDF, I want to attach a UTM_term parameter ?utm_term=customer1 and then have the PDF read this parameter and update the ctaButton url to https://mywebsite.com/?utm_term=customer1.
I've been messing around with the Javascript actions in Acrobat for a couple of hours trying to make this happen. Any help greatly appreciated.
You can get the full url to the document using...
var myURL = this.url;
"this" in Acrobat JavaScript is the document context.
I did hours of research and came to this conclusion – Javascript in Acrobat is like trying to code in 1985 AND browsers will not execute whatever code you come up with.
So I used this workaround:
When I send the PDF to someone, I send it as a link with a base64
encoded stringified JSON package that contains a bunch of tracking
data but importantly, the name of the file to access as well as utm
parameters specific to the recipient
The link hits a server handler (NodeJS) that extracts the encoded
JSON package, and uses the data in the package to serve up an HTML
redirect page pointing to the right PDF file
Importantly, the HTML page also saves the JSON package to the
browser's localStorage . . . this comes in handy in subsequent
steps
The PDF file opens in browser (it doesn't have to, could be opened on
desktop) and the call to action link has a link to a get request
handler
The get request handler serves up ANOTHER redirect page
This second redirect page accesses the browser's local storage, looks
for the utm parameters I set for that user, and then redirects to the
sale page, with nice utm parameters attached
So to sum up, you don't add the utm parameters to the call to action link in the PDF (because that would make the world too easy to live in) and instead you do all these acrobatics (no pun intended) to attach utm parameters in the link clicks (via JSON strings saved in localStorage) during the process (i.e. when user opens email to extract file via link, and then when user clicks call to action in the PDF).
Any questions or clarifications please let me know in the comments and I will do my best to address.
Caveats
Only works if user uses same browser in all steps (i.e. if Susan opens the email in Safari, saves the PDF, then clicks the call to action in the PDF, and the link opens in Chrome, utm parameters will not be passed).
Assumes browser is modern and has localStorage
UPDATE: I came across another solution. It's a bit more convoluted. Diagram below.
Porky.io is a Javascript extension for Adobe Indesign. So flow is:
send Porky.io the customer data you need (e.g. utm's for links)
Porky.io generates PDF from a template you provide with the customer data you provided
Listen for a new file save from Porky
Do something with the file (e.g. email it to customer)
I believe you need to run an instance of Windows somewhere in the cloud (e.g. on Azure) to run Indesign with the Porky.io. Unless you want to rely on your laptop.
My project's not big enough yet to warrant setting this up . . . but good alternative if I need to make my current solution more robust.

How to get POST contents before form submits using javascript

So, I know that when I submit a form whose method is POST that the server receives the contents of that form and then processes them accordingly, and then returns a page with the desired content. What I am trying to learn is what exact query url is being passed to the server side script when I submit a form on a website that does not belong to me. The reason I want this query string is so that I can make use of the server side script programatically with my own data. There is no public API served by this website, but I would like to formulate my own.
So my question is, is there a way to intercept the POST as a query string URL? Perhaps by using a javascript console in browser?
I know I can look at the source code for the page and find the names/values of the form fields. However, there also happens to be a hidden field on this page whose properties are set by javascript during validation at submission time. How should I go about this?
You can use an extension for intercept the data : Tamper Data on FireFox
https://addons.mozilla.org/fr/firefox/addon/tamper-data/
You can intercept and modify all headers requests

displaying the parsed result to user got after form post method

Assumption - people in my office are not that smart / I dont want to use any server port for my below utlity :)
My Html file resides in the local disk (consider cross domain refrences constraint hile sugessting the solution)
When double clicked , HTML form opens and asks for entering order number
when entered and form is submitted , server responds back with details in the form of XML
Note : for submitting i used html POST method ,
Upto 3rd step everything works fine and i get the server response xml as next page on browser.
Now,
How to parse this response xml so that only desired data is shown on the next page when form is submitted on 3rd step , bypassing the raw response from server.
JS submit function
function submit1() // called when i click on submit button on my form
{
var actionFianl = "http://URL(other data is given as form post method)";
document.form1.action = actionFianl;
document.form1.submit();
}
And XML data is displayed , starting with
<?xml version="1.0" encoding="UTF-8" ?>
So if I understand your issue, Ajax calls are out of the question due to cross domain trouble. I remember doing something like this and found that Safari worked, but Chrome did not like it, due to cross origin policy. This was a while back and I am not certain if it was the same problem.
If I understand you correctly you want to have the XML in Javascript, instead showing the raw data independently. What comes to mind if Ajax can't be used, is having two files. The current one will have the form included and gets the XML. The second file containing the first file in an iframe, which reads the content of the iframe, when the form is submitted, and is able to read the XML.
Another solution might be something like node-webkit depending on the scale of your application. Node-webkit is an executable for windows/mac/linux which ships Webkit and Node, and runs them in a single context. Maybe you could get the XML through the Node server instead doing it through a vanilla form submit.

How to download a filled in form in PDF format using javascript or html

I have a html page with a form ,with few input fields, check box,radio buttons.
User can fill up the forms.
Is there any way , the user will be able to download the form (with the filled in data) in pdf format ?
1 more question, Any way to save the filled in form in html format or image format (of course File-> save of browser and screenshot are there) using java script code or html code , on a button click ?
I have tried http://code.google.com/p/jspdf/ , didnot able to get through it.
Thanks
[Adding a few more points]
if generating a pdf file using JS/HTML is not possible then ....
A bit more into it
Currently I am creating a server using C# application and when user requests a html page , I am sending the same to user.
that html page contains a form , which is needed to be filled
and I want the user to save the filled form in pdf format.
For now , I am able to process the static html form -> convert to pdf and provide the download to user ,
But can't get a way to enable the user to save the filled in form.
When the user presses the submit button , I can get all the filled in data using httpResponce object(GET/Post Method)!! , is there a way I can generate a pdf file using this httpResponce object parameters?
Or any way I can send the current html page content (e.g. getting all the contents in a div using jquery/javascript) and send it back, when user presses the submit button, in that way I can generate the pdf file at server side and provide a download - PDF format
Please ask , if I am not able to describe my question !!
jsPDF is an open-source library written by our lead developer, James,
for generating PDF documents using nothing but Javascript. You can use
it in a Firefox extension, in Server Side Javascript and with Data
URIs in some browsers. http://snapshotmedia.co.uk/blog/jspdf
Using only javascript to generate the PDF is not possible at this time. Using server-side scripting, it is possible to send the data to a server and let the server generate the PDF which is then sent to the user.
(Update): Using PHP, you can generate PDFs in the server-side using dompdf - HTML to pdf converter. Here is a demo: http://eclecticgeek.com/dompdf/docs_0-6-0/demo.php
In Google Chrome, there is a "Print to PDF" option. If you don't want to do any programming on the server side, you can just ask your users to access the page using Google Chrome. After filling out the form, they print the page as PDF.

Categories