I wanted to make a generic system in my application where I could input a file of a readable type, let's say a Text Document
and convert it to a PDF
.
My research has lead me to the CLI
version of LibreOffice
which allows you to do just that, the conversion from a given file type to a PDF
, specifically, this is the command I found,
libreoffice --headless --convert-to pdf path/to/file
, this will automatically convert, if convertible the file that was specified and it will output a .pdf that contains, hopefully all the data.
Now, the end goal is to have the ability to specify a list of words which are to be highlighted within that document, specifically, highlight them with a yellow background.
This has led me to some things called Macros
and I've noticed I could write one in JavaScript
.
I created my JavaScript Macro
for LibreOffice
function highlight( words ) {
var document = XSCRIPTCONTEXT.getDocument();
var descriptor = document.createSearchDescriptor();
descriptor.setPropertyValue( "SearchCaseSensitive", true );
descriptor.setPropertyValue( "SearchWords", true );
for( let i = 0; i < words.length; i++ ) {
descriptor.setSearchString( words[ i ] );
let found = document.findFirst( descriptor );
while( !found ) {
let text = found.getText();
text.CharBackColor = "#FFFF00"; // Yellow Hex
found = document.findNext( found.getEnd(), descriptor );
}
}
}
So I tried to modify my command from above,
libreoffice --headless --norestore --invisible --convert-to pdf path/to/file macro:///home/macros.highlight(${words})
Notice that I run this command inside a container, there I have create a mount point which contains all my macros.
The command I'm showing above I have constructed programmatically using my Node.JS application.
Here is what I think are the problems.
- I don't think I understand when the Macro takes place, does it work before the or after the file has been convert to a PDF, does it actually do anything.
- Is my Macro actually loaded or not
- Does the command make sense at all, I can't seem to find a lot of useful resources online.