0

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.

Julien
  • 1,613
  • 1
  • 10
  • 26
  • Sure: find some non-R method out there (such as the `a2ps` command-line), and call it on that `.txt` file. All methods I'm aware of tend to rely on markup of some sort. What's your aversion to using rmarkdown? – r2evans Nov 25 '22 at 15:46
  • 1
    See here: – TarJae Nov 25 '22 at 15:49
  • 1
    @TarJae, I assume you're suggesting the use of `grid::textGrob`, is that right? – r2evans Nov 25 '22 at 15:57
  • @r2evans Yes indeed. – TarJae Nov 25 '22 at 16:00
  • As I remember with `grid::textGrob` it is easy to get text outside the visible area for such case. – polkas Nov 25 '22 at 16:32
  • 1
    On Windows `shell("notepad /p filepath")` where filepath is the filename including the path. It will print the indicated text file to the default printer which should be set to the pdf printer. It will issue a popup requesting the output file name. Notepad also has a /pt argument that can be used to specify the printer. – G. Grothendieck Nov 25 '22 at 17:13

1 Answers1

0

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

enter image description here

However if WordPad was last used like this

enter image description here

we get similar output enter image description here

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.

enter image description here

now we get the desired layout

enter image description here

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"')

K J
  • 8,045
  • 3
  • 14
  • 36