Is this possible to convert a .txt file to a PDF in R without using RMarkdown?
In this question, a solution is given but it relies on RMarkdown.
Is this possible to convert a .txt file to a PDF in R without using RMarkdown?
In this question, a solution is given but it relies on RMarkdown.
Kudos to @G.Grothendieck for reminding me of the native windows method to convert txt to pdf, so simply shell out but beware how quotes are needed. It can often be simpler to store the line in a BATch file and invoke that with just filename.
"write /pt "path\filename.txt" "Microsoft Print to PDF" "Microsoft Print to PDF" "path\filename.pdf" "
and that is not a typo for MS.pdf both driver and printer are the same syntax !
but if you want the normal print dialog simply use,
"write /pt "path\filename.txt" "Microsoft Print to PDF"
There are some very limited means to control output such as save a file in WordPad with styles margins etc. But there are also ways to mess about in the registry values for WordPad if you want much more control. Even in notepad you can skew lines by one degree for an April 1st jape on colleagues.
For more detail see https://stackoverflow.com/a/70221226/10802527 and the postscript discussion https://stackoverflow.com/a/67696473/10802527
The cmd could run as a shortcut for "drag and drop" or right-click "send to" or as batch file or as a line for Python to use. The way you write the line for variables will be related to desire of method. so simplest is put that single line in a file and save as follows.
txt2pdf.bat
if exist "%~dpn1.pdf" echo:&echo %0 Will not overwrite "%~dpn1.pdf" &pause&goto end
write.exe /pt "%~1" "Microsoft Print to PDF" "Microsoft Print to PDF" "%~dpn1.pdf"
:end
we can drop a file.txt onto that bat and expect to get a pdf
However if WordPad was last used like this
so before running a batch the first print layout especially margins must have run as landscape or portrait so print a dummy.pdf then close wordpad.
now we get the desired layout
Back to the core question part, so in r or python or any language you simply need to OS call the cmd in desired sequence ??
"path to/txt2pdf.bat" "path to/a variable name.txt"
I dont currently run r to test but understand it should be similar to
shell('"path to/txt2pdf.bat" "path to/a variable name.txt"')