Text to pdf conversion

I am presently using soffice --convert-to “pdf” but I am having problems with some font sizes.

Is there a way to specify the font size on the command line?
I have searched on the internet without any success.

The other option would be to use ghostscript but also cannot find the command line options to use.
Any help would be appreciated.

I use it with neovim, but changing the fontsize works.
I have a script called tpdf:

#!/bin/bash
file=$(readlink -f "$1")
base="${file%.*}"
nvim $file -c "set printfont=courier:h$2" -c "hardcopy > $base.ps | q"
ps2pdf $base.ps && rm $base.ps

Usage: ./tpdf file.txt 12 where ‘12’ is the fontsize.

PS: Installation of nvim [as @Hanzel pointed out]:
sudo pacman -S neovim

vim works just as good as neovim in this case.

1 Like

It appears than nvim is not part of manjaro.
How do I install it?

There is neovim in the repo’s that can be started as nvim so that seems to be the package you need in this case.

Did you have a look to Pandoc ? :thinking:

https://pandoc.org/

Thank you for the options.
They all need installation.
I would like to use something that is already installed.

soffice, which is part of Libreoffice and part of the Manjaro installation, converts txt to pdf fine, I just don’t know how add the font size to the command line. I find it hard to believe that the developers of Libreoffice did not include an option for font size because they believe that anyone who wants to convert to pdf, all want the same font size. There must be a way to do this, but can’t find the answer on the internet.

vi (a similar, but simpler editor) can do the same - vim as well
one of those is likely already present
if not - these are tiny programs

Of course it can do that - just not via the simple command you want to use.

1 Like

Out of programs suggested only vi is present in my Manjaro installation.

What would be the command line be for using vi to convert *.txt to *.pdf with a specified font size?
I think vi is just an editor.

It is - and so is vim and neovim. :sunglasses:
From what I have seen: it would work exactly the same with vi as with nvim - as @Shirshendu posted.
You can use a simple commandline instead of the script as well, but the script is more versatile.

LibreOffice wasn’t built for command line usage for the most part, and the convert flag is primarily there so you can convert odf files (which are rich text, i.e. already have specified font sizes) to pdfs. The fact that it works on plain text files is just a bonus. So no, there are no command line options.

If you really must only use LibreOffice, you can acomplish what you want with a macro which you can just invoke from the command line similar to --convert-to.

The macro basically does this:

  1. Opens the file with the given path
  2. Selects all text
  3. Sets font size to size specified in the argument
  4. Exports file as pdf
  5. Closes the file

e.g.

sub ToPDFWithFont (F as String, FSize as Integer)
rem F = "/tmp/test.txt"
TargetURL = convertToURL(F)
MyDoc = StarDesktop.loadComponentFromURL(TargetURL, "_blank", 0, Array())
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = MyDoc.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SelectAll", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(2) as new com.sun.star.beans.PropertyValue
args2(0).Name = "FontHeight.Height"
args2(0).Value = FSize
args2(1).Name = "FontHeight.Prop"
args2(1).Value = 100
args2(2).Name = "FontHeight.Diff"
args2(2).Value = 0

dispatcher.executeDispatch(document, ".uno:FontHeight", "", 0, args2())

docURL=MyDoc.getURL()
docURLsplit        = Split(docURL, ".")
docExt             = docURLsplit(Ubound(docURLsplit))
pdfURL             = Left(docURL, Len(docURL) - Len(docExt) -1) & ".pdf"

Dim storeArgs(0) As New com.sun.star.beans.PropertyValue
storeArgs(0).Name  = "FilterName"
storeArgs(0).Value = "writer_pdf_Export"
MyDoc.storeToURL(pdfUrl, storeArgs())
MyDoc.close(True)

end sub

You should be able to just drop the above macro into your macros by going to Tools>Macros>Edit macros and pasting it in Module1 macro file under “My Macros and Dialogs”.

And then call it from the command line thusly:

libreoffice --headless 'macro:///Standard.Module1.ToPDFWithFont("/path/to/my/file.txt", 24)'

1 Like

belyash, this is brilliant suggestion.
I followed your instructions and works fine from the terminal creating the .pdf file in the same folder as the .txt file.

Unfortunately, as I am trying to use it in a TCL script as:
exec libreoffice --headless ‘macro:///Standard.Module1.ToPDFWithFont($doc_name.txt, 10)’

Where doc_name evaluates to the text file location with no spaces.
It takes a little while like it was creating the pdf file but the file is not there. No error is produced. I cannot locate the resultant pdf file in any other folder either.

Maybe I need to start another post for this for an expert in tcl programming.

AFAIK my macro only works with an absolute path. I wrote it just for you and didn’t bother figuring out how to make relative paths work in OpenOffice/LibreOffice BASIC :slight_smile:

I have a feeling $doc_name is probably not an absolute path and is causing your issue. LibreOffice won’t show any errors if running in headless mode without a GUI.

No, I assumed that absolute paths are required and $doc_name.txt evaluates to the absolute path of the text file. I don’t need relatives paths.
I didn’t know that about the headless mode without the GUI.

Then I’d suggest printing out the command which you are trying to exec in tcl and run it yourself in the command line without --headless and see if LibreOffice gives you any error pop ups with the GUI.

Running the script from inside the tcl script without the --headless gives the error as attached.
The .txt file shown is correct.
Also repeating what I stated before, running this in terminal works fine.
Screenshot_2023-02-14_21-14-16

Just noticed that clicking the above OK produces the following popup:

Screenshot_2023-03-14_08-00-51

Of course that …Ledger/10 does not exist. Why is it looking for that?

From the first pop-up, it seems like somehow tcl is probably running:

libreoffice /mnt/Data/Dropbox/Data/APPS/Ledger/'macro:///Stardard.Module1.ToPDFWithFont(/mnt/Data/Dropbox/Data...

and treating the comma separated 'macro://string, 10' as an array it needs to append to the base path…

Maybe try making sure your Tcl is quoting the path with "? Otherwise, I think this has become more of a Tcl debugging question and you should start a new thread.

P.S.

Please copy text whenever possible instead of posting screenshots on the forum. Rules here:

My apologies for posting the screenshots.

Thank you balyash for your patients and especially for wring the macro.

I agree, This problem now belongs in tcl.
I will start a post in due course.
Thank you everyone else in trying to solve problem.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.