chrome_print('foo.html') doesn't work, which is understandable, but file://full/path/to/foo.html doesn't work, either. So how can we print local HTML files?
Of course, we can launch a local server (e.g. servr::httd()), but I wonder if there are other ways.
Just to share that local printing is working for me on windows. It was working with crrri and I just check with pagedown
pagedown::chrome_print('file:///C:/Users/chris/Documents/test.nb.html', verbose = T, browser = chrome_bin)
gets me a _test.nb.pdf_ file in the current working directory. I鈥檒l check this afternoon if polyfill is ok or not.
Also, just to add that, the _CLI tool_ from pagedjs project to print pdf is using a local server to serve the page before printing it. (if even serve the js polyfill script)
Same thing for me on ubuntu only with self contained html document.
copy_template = function(name) {
rmd_file = sprintf('%s.Rmd', name)
file.copy(
system.file('rmarkdown', 'templates', name, 'skeleton', 'skeleton.Rmd', package = 'pagedown', mustWork = T),
rmd_file,
overwrite = TRUE
)
rmd_file
}
print_template = function(name) {
output = rmarkdown::render(copy_template(name))
pagedown::chrome_print(paste0('file://', output), verbose = T)
}
This works well:
print_template('business-card')
However, this does not work:
print_template('html-paged')
This is due to security restrictions. Opening the html-paged.html file with Chrome, I get the following error:
Fetch API cannot load file:///home/.../html-paged_files/paged-0.1.4/css/default-fonts.css.
URL scheme must be "http" or "https" for CORS request.
ok. there is something going on with local html and paged.js.
pagedown::chrome_print('file:///C:/Users/chris/Documents/DEV_R/pagedown/inst/examples/index.html', verbose = T, browser = chrome_bin)
timeout because the pagedownListener binding event is not reveived. The js script are not correctly found and executed.
So it is working for simple and standalone file, but not from html file that a ressource folder.
In fact the error comes from CORS request that does not support file:// scheme but only http or https. I see those errors just if I am trying to open the local html file generated in chrome. I can't open it properly because the _Fetch api_ throws an error.
paged.js:23887 Fetch API cannot load file:///C:/Users/chris/Documents/DEV_R/pagedown/inst/examples/index_files/paged-0.1/css/default-fonts.css. URL scheme "file" is not supported.
I think serving the local file may be the clever thing to do here.
There is yet an option for chromium : --allow-file-access-from-files but is does not seem to help on this one as it does not seem to be chromium that blog but the fetch API.
I will keep on digging.
PR #71 should fix this issue. @cderv Please also feel free to review the PR. Thanks!
Oh thanks! I did not had time during this week. Sorry for that.
Thanks @RLesur for the review.
No worries at all. Please don't feel obligated to any tasks I assign to you. If there is anything urgent, I'll just move on without waiting. Usually nothing is urgent, though.
Most helpful comment
ok. there is something going on with local html and paged.js.
timeout because the
pagedownListenerbinding event is not reveived. The js script are not correctly found and executed.So it is working for simple and standalone file, but not from html file that a ressource folder.
In fact the error comes from CORS request that does not support
file://scheme but only http or https. I see those errors just if I am trying to open the local html file generated in chrome. I can't open it properly because the _Fetch api_ throws an error.I think serving the local file may be the clever thing to do here.
There is yet an option for chromium :
--allow-file-access-from-filesbut is does not seem to help on this one as it does not seem to be chromium that blog but the fetch API.I will keep on digging.