Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
805 views
in Technique[技术] by (71.8m points)

r - How to convert docx to PDF?

I want to ask if it is possible to convert text files such as word document or text document to PDF using R ? I thought of converting it to .rmd and then to PDF using this code

require(rmarkdown)
my_text <- readLines("C:/.../track.txt")
cat(my_text, sep="  
", file = "my_text.Rmd")
render("my_text.Rmd", pdf_document())

But it doesn't work showing this error:

> Error: Failed to compile my_text.tex.
In addition: Warning message:
running command '"pdflatex" -halt-on-error -interaction=batchmode "my_text.tex"' had status 127 

Is there any other solution ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.txt to .pdf

Install wkhtmltopdf and then from R run the following. Change the first three lines as appropriate depending on where wkhtmltopdf is on your system and depending on the input and output file paths and names.

wkhtmltopdf <- "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
input <- "in.txt"
output <- "out.pdf"
cmd <- sprintf('"%s" "%s" -o "%s"', wkhtmltopdf, input, output)
shell(cmd)

.docx to .pdf

Install pandoc, modify the first three lines below as needed and run. How well this works may vary depending on your input.

pandoc <- "C:\Program Files (x86)\Pandoc\pandoc.exe"
input <- "in.docx"
output <- "out.pdf"
cmd <- sprintf('"%s" "%s" -o "%s"', pandoc, input, output)
shell(cmd)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...