Auto Print PDF to Printer on Button Click

I currently have an action button for Print which on click generates the PDF exporter which within chrome auto downloads to the browser. Currently I need to click to open the pdf and than use computer browser print to print which once again adds the step to click print on the print preview

Is there a way to auto print the PDF generated from PDF exporter

Hey @seandawes!

I don't believe there is a way to directly print from within Retool. Since all JS in Retool is run in a sandbox, so a lot of window functions (like window.print() )won't work here. You can however use window.print() in a custom component if you toggle on "allow modals" in the component inspector and pass your PDF to the component through the model but this would print the full custom component iframe, you may be able to use a JS library from here, but I haven't had any luck yet with my current setup using printJS

@Chris-Thompson Thanks. Its not so much I guess a big deal when using an ipad given ios but just trying to reduce steps and basically making a print packing slip function

Hey @seandawes — I see full transparency here, at the moment, mobile development has not been a primary focus. We do have a team now dedicated to mobile development so hopefully, this will be even better soon! In the meantime, I believe the best approach for building out an easily customizable packing slip would either be the window.print() in a custom component or possibly an external API. Do you think either of these could work for your use case here?

Hey seandawes,
I am unable to get in pdf format. how did you do that. can you guide me. i can generate doc or json or csv but not pdf.

Hey @Geetha2021!

If you are looking to download a PDF of a portion of your app, you can use utils.downloadPage() and pass this function two params, the first would be a string representing your desired file name and the second is an object of additional optional params. Here's a link to our docs on this that could be helpful here. Let me know if you have any questions! :slight_smile:

@Chris-Thompson

I think I can just achieve this using the pdf exporter as on mobile like ipad its not that bad to click to print. I tried doing the utils.downloadPage() but not a fan

Had a question. So my goal is printing packing slips for orders. So I have basically a SQL query which shows all order contents. If the order contains 1 item, its nice and clean in the pdf exporter, but if the order contains more than 1 row of data in my sql query it is combining them by column.

Example:

Product 1, Product 2, Product 3
Sku 123, Sku 456, Sku 678
1,1,1

Being product name, sku and qty

I would want it to come over as

Product 1
Sku: 123
Qty: 1

Product 2
Sku: 345
Qty: 1

etc

Here is my snippet for pdf

Order Number: {{s_table.selectedRow.data.number}}

{{packing_slip.data.Name}}
SKU: {{packing_slip.data.sku }}
QTY: {{packing_slip.data.qty}}

Hey @seandawes, stepping in for Chris here. :nerd_face:

So to get the format you want, you can use a transformer to format the data the way you need it. Based on what you are returning now, it looks like you'd need to iterate through the packing_slip data and utilize i to group the name, SKU, and quantity as desired.

Here's an example of what it could look like when you're keying into that array:

Name: {{packing_slip.data.Name[i]}} SKU: {{packing_slip.data.sku[i]}} QTY: {{packing_slip.data.qty[i]}}

@Jay Sorry still not following.

What would go in the transformer and what would go in the query of the pdf exporter?

Hey @seandawes, could I see some screenshots of how you have things set up right now with the SQL query and the pdf exporter? I'd be happy to send some examples based on your specific use case!

@Jay Sure

SQL which is taking the order number and pulling all data for that order number. So an order can contain more than 1 item. SQL is triggered on click of PRINT button which success of SQL triggers pdf exporter

PDF exporter was this

But that mashes all the product names together than all the skus together and etc

@Jay Figured I would see if you had any suggestions

Hey seandawes, I'll be stepping in for Jay here and I apologize for the very long delay. If I'm understanding your problem correctly, then you can use a JS query to create a long string that you can pass to your PDF Exporter. This query will allow you to programmatically separate out multiple items if multiple items are present. Here is an example of what that could look like:

In my JS query, I first make sure that my data is formatted as an array of objects (using formatDataAsArray) if it isn't already in that format. Then I loop over each object in the array and concat the values with their headers to a string with newline characters as needed. Finally I trigger my PDF export query with query.trigger() passing the string to the exportPDF query using additionalScope. In my exportPDF query I can reference the str variable defined in additionalScope. It will show up as undefined in the query input but when I trigger that query using query.trigger() and pass str as a key, value pair in additionalScope, the value will be defined.

I hope this helps for your use case!