AppsScript generated PDF render quality. Can it be upped?

189 views
Skip to first unread message

Jonathan Vargas

unread,
Aug 29, 2019, 9:03:42 AM8/29/19
to Google Apps Script Community

I have a AppScript based data reporter that takes information from Sheets, builds a full template-based report on Docs, calculates and analyzes the data then generates charts and adds them to the report. At the end it takes the finalized report from Google Docs, and uses the MailApp to send a email with the attachment using file.getAs(MimeType.PDF). 

All in all it works beautifully for my clients needs per functionality, so no issues there. The one request I've been getting from my client lately is whether I can up the quality of the graphs rendered in the PDF. I've been playing around with the Charts service, pre-building the blob on Sheets, etc. But the final render of the chart seems to stay slightly blurred in the PDF even if the Docs backup file shows a nice, crisp image. 

All in all, I wondered if anyone has been able to come up with any method to up the render quality of a AppsScript generated PDF. 

Dimu Designs

unread,
Aug 29, 2019, 7:33:31 PM8/29/19
to Google Apps Script Community
I don't believe its possible to improve the render quality using the PDF export options native to Apps Script. By default, the PDF export endpoints renders charts and graphs as raster images where ideally you'd want them to be rendered as vector images. You'd probably have to leverage some third party service or build your own PDF generator and expose it as a service. 

Jonathan Vargas

unread,
Aug 29, 2019, 7:58:25 PM8/29/19
to Google Apps Script Community
I was afraid of that. My research pointed me in that same direction. I'll experiment with a few things but much as you say I may be forced to use a third party item. thank you for the outside input. 

Clark Lind

unread,
Aug 30, 2019, 11:00:29 AM8/30/19
to Google Apps Script Community
I'm trying to find an ugly hack I put together a few months ago. It involved adding the charts to Slides, then using the Slides Export function to create PNG files of each chart, and then I placed the chart within a Site. They were cleaner and crisper. So I imagine you could probably do the same sorta thing. Essentially: create better graphics first, then embed into the Doc prior to converting to PDF. If I can find the hacky code, I'll post it.

Clark Lind

unread,
Aug 30, 2019, 11:27:53 AM8/30/19
to Google Apps Script Community
Here it is. Your mileage may vary...  ;)

function getChartToPNG() {
 
var slides = SlidesApp.openById('YOUR SLIDES ID');
 
var slide = slides.getSlides()[0];  //get the first slide. if you want all the
 
var charts = slide.getSheetsCharts(); //gets charts embedded from or linked from sheets
 
var image = DriveApp.createFile(charts[0].asImage()).setName("chart.png");  //create a new file in Drive. You could do a lot more here,..
 
//putting images in a specific folder and over-writing existing image files, and if they were already embedded with docs, you may be done.

//from here, I inserted the image into my Classic Google Site.
 
//var page = SitesApp.getPageByUrl('URL TO My SITE');
 
//var atch = page.getAttachments();
 
//atch[0].setFrom(image);  

//So I assume you should be able to do something similar with Docs.

}

Jonathan Vargas

unread,
Aug 30, 2019, 2:28:24 PM8/30/19
to Google Apps Script Community
By casting to to MimeType, I can use the .GetAs() call and pull it as a .png, .jpg or other such image, that is not necessarily the problem. The problem is along the lines that when the image is taken through the service that build up the PDF, the PDF will loose some of the contrast and sharpness as it captures the image. This can usually be dealt with by using high resolution vector images but, I don't believe google has a back end service that is able to produce those type of images. I've been attempting to actually reproduce a similar vector build using existing services but the problem is that vector images take a high amount of processing time and it simply times out. Thank you though.

Clark Lind

unread,
Aug 30, 2019, 3:33:42 PM8/30/19
to google-apps-sc...@googlegroups.com
I was hoping by starting with sharper images, the reduction in the conversion to pdf might make them come out a bit better. Don't know if it actually would though.. lol  Please post if you get something to work!

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/PPwedmWH-ik/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/fe09b31e-2bd9-4b06-9604-fd5ff9628314%40googlegroups.com.

Jonathan Vargas

unread,
Sep 4, 2019, 10:40:15 AM9/4/19
to Google Apps Script Community
That would technically be the right approach but, the images created for charts on all GSuite services use the same background service. It means that the overall final "Blob" file would be the same. The main difference is the render speed if it's pre-made versus calculated and built during script run. This affects the quota for the run time since it takes longer to make the chart internally. I did, however, attempt to make a code side build of the chart and them take it into the report but it didn't make that much of a difference. It was better, but not by much. 


On Friday, August 30, 2019 at 3:33:42 PM UTC-4, cwlind wrote:
I was hoping by starting with sharper images, the reduction in the conversion to pdf might make them come out a bit better. Don't know if it actually would though.. lol  Please post if you get something to work!

To unsubscribe from this group and all its topics, send an email to google-apps-script-community+unsub...@googlegroups.com.

Riël Notermans

unread,
Sep 5, 2019, 3:24:19 AM9/5/19
to Google Apps Script Community
Adding a service like this with cloudfunctions would give you great flexibility?


Have apps script send data, build the chart serverside and return the blob.





Op wo 4 sep. 2019 16:40 schreef Jonathan Vargas <jvargas...@gmail.com>:
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/b367d660-97bd-42da-8cc9-598eb14d0828%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages