handle_read_frame error: asio.system:10054
Hello, I'm attempting to use chrome_print() in a function that saves a gt_tbl object as html and as pdf. When looping through multiple gt_tbl objects, I'm getting this error message, and I don't know where it's coming from.
[2025-06-24 16:39:52] [error] handle_read_frame error: asio.system:10054 (An existing connection was forcibly closed by the remote host.)
This error doesn't show up when I don't use the gt_to_pdf() function on its own. It seems to only happen when I'm using it in a loop.
The directory I'm saving to is on Sharepoint/OneDrive.
The pdf files are created just fine even though the error message shows up on about half of the loops. But, I ran into an issue where RStudio fatally crashed after receiving this error several times.
This is the function I'm using:**
gt_to_pdf <- function (gt,fname, html_dir = paste0(getwd(),"/"),scale = 1, render_exist = FALSE){
if ("gt_tbl" %!in% class(gt)){
stop("gt argument must be a gt object")
}
gt_html = gt%>%as_raw_html()
gt_html_fpath = html_dir%&%fname%&%".html"
gt_pdf_fpath = html_dir%&%fname%&%".pdf"
write_file(gt_html,gt_html_fpath)
if (requireNamespace("pagedown", quietly = TRUE)) {
if (!is.null(fname)) {
pagedown::chrome_print(input = gt_html_fpath,output = gt_pdf_fpath, options = list(scale = scale),timeout = 45,format = "pdf",verbose = 0)
}
else {
stop("specify the filename you want to use. Do not include suffix")
}
}
else {
stop("Please install.packages('pagedown')")
}
}
Here's how I'm using the function in the loop:
for (j in 1:length(programs_of_study)){
program = gsub(" ","",remove_punctuation(programs_of_study[j]))
l_part_a = get(program%&%"_A")
l_part_b = get(program%&%"_B")
l_part_c = get(program%&%"_C")
l_html = l_part_a%>%
tab_footnote(footnote = l_part_b)%>%
tab_footnote(footnote = l_part_c)
gt_to_pdf(l_html,program,html_dir = html_path)
}
I don't know exactly what these errors mean, but this is coming from Chrome, which closed the WebSocket connection unexpectedly.
As you are iterating to print several pages, maybe you should not use chrome_print and consider using chromote directly (https://github.com/rstudio/chromote/)
You could then open a single chrome instance in background, have one communication with it, and iterate on all your saved document to load them in the single chrome instance, and trigger the print, then do the next.
chrome_print will open and close chrome after each document which is less efficient, and maybe there are timing issues.
As see you are also using this to print gt table. Have you considered gt own function ? See https://gt.rstudio.com/reference/gtsave.html
We can create an image file based on the HTML version of the gt table. With the filename extension .png, we get a PNG image file. A PDF document can be generated by using the .pdf extension. This process is facilitated by the webshot2 package, so, this package needs to be installed before attempting to save any table as an image file. There is the option of passing values to the underlying webshot2::webshot() function through .... Some of the more useful arguments for PNG saving are zoom (defaults to a scale level of 2) and expand (adds whitespace pixels around the cropped table image, and has a default value of 5), and selector (the default value is "table"). There are several more options available so have a look at the webshot2 documentation for further details.
So you can do PDF using gtsave()
Hope it helps.
Do do more debugging, having more verbose output will be required.