Question

i'm trying to using R and RApache to download pdf file with no success.

Here's my code

#R-Downwload pdf
#---------------------------------------------
pdf("/public_html/upload/rpdf.pdf")
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("PDF Report")
dev.off()

setContentType(type='application/pdf')
setHeader(header='Content-Disposition', 'attachment; filename=rpdf.pdf')
setHeader(header='Content-Transfer-Encoding', 'binary')
setHeader(header='Accept-Ranges', 'bytes')

cat("/public_html/upload/rpdf.pdf")

When I run this code on the browser, it show me the download popup box but when I click to download it display

... either not a supported file type or it has been damaged"
Was it helpful?

Solution

If you ever come accross this question, here's the answer.

setHeader(header='Content-Disposition', 'attachment; filename=rpdf.pdf')
setContentType("application/pdf")
t <- tempfile()
pdf(t)
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("PDF Report")
dev.off()
setHeader('Content-Length',file.info(t)$size)
sendBin(readBin(t,'raw',n=file.info(t)$size))

Enjoy!

OTHER TIPS

You're printing out the string "/public_html/upload/rpdf.pdf" since it's in quotes. You want to print the content of the file, not the name of the file in your cat() call. Perhaps you could use write?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top