Synchronous pdf generation through pdfmonkey - pdf-generation

I am using pdfMonkey to store PDF template and generate PDF, I am calling pdfMonkey API mentioned in below link for generating pdf. I am getting success response but download_url is coming as null and preview_url is also returning 400. In order to fetch the PDF download url, I have to call get document API. So I have to do couple of API calls to generating a PDF. Is there any way I can generate PDF in single API call?
https://docs.pdfmonkey.io/references/api/documents#generating-the-document-upon-creation

Related

Mock in Django a Binary file retrieved from an API

I'm using Django and DRF, and I have a view that retrieves a Binary File (Using BinaryFileRenderer) from an external API. This external API brings the binary data from S3.
I need to mock this call so I could test the logic by isolating the API call, but every time I tried I received some error. For example, I tried creating a MockFile class with content, mime_type, and name fields. And I started to receive the error "AssertionError: renderer returned Unicode, and did not specify a charset value."
After some investigation, apparently, it was expecting some kind of Renderer file. But this is when I got confused. Because Python is dynamic, I'm never sure what kind of file I received and I'm not sure how I can mock the binary file I'm receiving.
Anyone can point me to how correctly mock this binary file?

How to send local image file via an api to parse.com?

I am trying to build up a web application with javascript and nodejs. In order to hide the ApplicationID/Key, I use API to handle the data saving.
For example, if I need to save a object on Parse. Instead of using Parse Javascript SDK on the client side, I send the object to the server and then ask server to save the parse.
Everything was working fine until I try to upload images to the server. It turns out I need to somehow upload the images to server before I can save these images to parse class because PFFile need urls to upload the images. But the image at this time is still in local. I was thinking to convert image to base64 string and then server can convert it back to image data and then save it to the parse. However, I didn't succeed with this approach. Can anyone provides some insight? Thanks
When uploading a image, you can just use
uploadImg(photo):void{
var parseFile = new Parse.File("image.png", {base64:photo});
parseFile.save();
}
the parameter photo is base64.
*Are you using AWS s3 to store your images?

Problem with Google AJAX Search API RESTful interface

When I send the following query
http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=coffee%20New%20York%20NY
using c# WebClient.DownloadString function or ordinary web browser
I get JSON data which is different from data for the same query using JavaScript and
Google AJAX Search API.
From REST service I get the following url field
http://www.google.com/maps/place?source003duds0026q003dcoffee0026cid003d13245583795745066822
but from JavaScript query I get this url field
http://www.google.com/maps/place?source=uds&q=coffee&cid=13245583795745066822
The problem with REST service answer is that the url it gives points
to a web page with error message "We currently do not support the location".
What am I doing wrong?
It looks like either you are decoding the URI from the REST request incorrectly, or Google is ending it wrong. The = are being sent or parsed as 003d and the & as 0026
EDIT : After trying that link I see they return the links, in the JSON, with the '=' and '&' encoded, the JavaScript must be replacing those characters for you automatically. You could do a simple String replace on "003d" and "0026" - although I'm not sure that will cover every use case.
I resolved this by parsing JSON data with Json.NET library http://json.codeplex.com/

Generating PDF's via Delayed Job while maintaing a RESTful pattern

currently I am running a Rails app on Heroku, and everything is working great with exception of generating PDF documents that sometimes contain thousands of records. Heroku has a built-in timeout of 30 seconds, so if the request takes more than 30 seconds, it's abandoned.
That's fine, since they offer delayed_job support built-in. However, all of the PDF's i generate follow a typical restful pattern. For instance, a request to "/posts.pdf" generates a pdf (using PRAWN and PRAWNTO) and it's delivered to the browser.
So my basic question is, how do I create dynamically generated PDF's with delayed_job while maintaining the basic RESTful patterns Rail's so conveniently provides. Thanks.
You could do something like:
Send a request to generate the pdf: POST /generate_new_pdf
Have that action return the ID of the new pdf before it's created
Poll the endpoint for that resource ID until it's done (returning 202's in the meantime): GET /pdfs/:id

Downloading CSV via AJAX

Can you use AJAX to download a generated csv file from a web application? If so does anyone have any kind of reference that I could be pointed towards?
EDIT: Sorry I should have mentioned I am using Prototype's Ajax.Request and I looked in firebug's response tool and the generated CSV is the response, I just need to get it to pop up with the save file option after has been generated by the Ajax.Request
This is a known limitation of Ajax requests, you will need to use JS like:
window.location='download-csv.rb';
Instead of using an Ajax request. Another way is to change the location of a hidden Iframe, but this has it's own pro's/con's.
You will never get an Ajax request to display the 'file save' dialog, no matter what HTTP headers you send.
In light of your latest edit, to make your CSV file trigger a file download (instead of rendering in the browser), there's no need for Ajax.
Instead, the solution is to have your back-end system add this HTTP header when the CSV file is requested:
Content-disposition: attachment; filename=<your_filename.csv>;
Your implementation here depends on the back-end system you're using. If you're using Rails (as your username suggests), here's a start:
filename = 'your_filename.csv'
headers['Content-Type'] = 'text/plain'
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
render :layout => false
Downloading it isn't the problem; you can download any data you like via XmlHttpRequest. The hard part is parsing it. There are several ways to parse it, from regexs to string indexing.
You can use "AJAX" to download anything .. Some people would say you shouldn't call it AJAX in that case since that term is rigorously devoted to downloading XML. But really it's just a mechanism to get data into the client w/o reloading a page. If you were loading HTML it'd be called AHAH, for CSV i guess you'd call it AHAC or AJAC? ..

Resources