General TC > Programming & Scripting - Unofficial

driverless, CUPS-less way to print PDF file

<< < (2/2)

GNUser:
Hi gadget42. Thank you for clearing me of any wrongdoing :)

I did discover two more valuable pieces of information since my last post:

1. libreoffice can convert almost anything into a pdf, even image files (e.g., jpg). The result is that the print script (version 2.0 in reply #3) has been able to successfully print every filetype I've thrown at it so far.

2. The libreoffice developers provide an AppImage and it works perfectly on TCL. It's a single file with no dependencies (other than the usual tweaks for AppImage support on TCL), which seems magical when you consider that libreoffice.tcz contains 3,500 files and its dependency tree looks like this.

If you discover any new pearls in your experiments, please do share!

patrikg:
I can also add some value to this, you talked about libreoffice.
You can also batch convert files with libreoffice to pdf or another file format.
Use the command line option --convert-to

I have done it many times to convert hole directories with lots of doc's files.
And what great with this is that the program get's headless, you don't need any windows system.

Cut from the help.

--- Code: ---   --convert-to OutputFileExtension[:OutputFilterName] \                     
     [--outdir output_dir] [--convert-images-to]                               
                       Batch convert files (implies --headless). If --outdir   
                       isn't specified, then current working directory is used
                       as output_dir. If --convert-images-to is given, its     
                       parameter is taken as the target filter format for *all*
                       images written to the output format. If --convert-to is
                       used more than once, the last value of                 
                       OutputFileExtension[:OutputFilterName] is effective. If
                       --outdir is used more than once, only its last value is
                       effective. For example:                                 
                   --convert-to pdf *.odt                                     
                   --convert-to epub *.doc                                     
                   --convert-to pdf:writer_pdf_Export --outdir /home/user *.doc
                   --convert-to "html:XHTML Writer File:UTF8" \             
                                --convert-images-to "jpg" *.doc             
                   --convert-to "txt:Text (encoded):UTF8" *.doc             

--- End code ---

GNUser:
Hi patrikg. libreoffice is used in my script only once, with that flag:

--- Code: ---libreoffice --convert-to pdf "$arg" --outdir $PDF_DIR
--- End code ---

patrikg:
Sorry @GNUser, I tldr your script. :)
By the way, I think also I could suggest another program that uses chrome/chromium to build presentations and you can also convert them to pdf.

https://marp.app/
https://github.com/marp-team/marp-cli/releases

Used that with vagrant and scripted a test script to do some performance test between uses intel nic vs virtio nic in a VM.

Here you go :)


--- Code: ---vm_box_name = ["Server", "Client"]
vm_name = "Ubuntu 22.04.1 LTS"
vm_host_name = "ubuntu2204"
nicname = ["Intel", "VirtIO"]
iperf3dir="/home/vagrant/iperf3/"
ip="192.168.56."

Vagrant.configure("2") do | config |
(0..1).each do | nictype |
(0..1).each do | sc |
config.vm.define vm_box_name[sc] + "-" + nicname[nictype] do | node |
node.vm.box = "ubuntu/jammy64"
node.vm.box_check_update = false
node.vm.hostname = "#{vm_box_name[sc]}#{vm_host_name}".downcase
node.vm.synced_folder ".", "/vagrant", disabled: true

if sc == 1
node.vm.synced_folder ".", iperf3dir, owner: "vagrant", group: "vagrant"
end

if nictype == 0
node.vm.network "private_network", ip: ip + ((nictype+1) * 10 + sc).to_s
else
node.vm.network "private_network", ip: ip + ((nictype+1) * 10 + sc).to_s, nic_type: "virtio"
end

node.vm.provider "virtualbox" do | vb |
vb.name = "#{vm_name} #{vm_box_name[sc]} #{nicname[nictype]}"
vb.check_guest_additions = false
vb.memory = 2048
end

node.vm.provision "shell", name: "Startup", keep_color: true, inline: <<-SCRIPT
#/usr/local/bin/env bash
iperf3dir="#{iperf3dir}"

title=("Private Network Intel Nic" "Private Network VirtIO Nic")

if [ "#{sc}" == "0" ]; then
if [ ! -f updateinstallflag#{sc}#{nictype} ]; then
echo "Updating local repo directory"
apt -y update

echo "Installing iperf3"
apt -y install iperf3

touch updateinstallflag#{sc}#{nictype}

echo "Starting iperf3 in background"
(iperf3 -s -1 && systemctl poweroff) &
exit 0
else
echo "Starting iperf3 in background"
(iperf3 -s -1 && systemctl poweroff) &
exit 0
fi
else
if [ ! -f updateinstallflag#{sc}#{nictype} ]; then
echo "Updating local repo directory"
apt -y update

if [[ "#{sc}" == "1" && "#{nictype}" == "1" ]]; then
touch updateinstallflag#{sc}#{nictype}

echo "Installing iperf3 chromium-browser jq"
apt -y install iperf3 chromium-browser jq

echo "Installing marp-cli"
curl -s -O -L https://github.com/marp-team/marp-cli/releases/download/v2.3.0/marp-cli-v2.3.0-linux.tar.gz
tar -xf marp-cli-v2.3.0-linux.tar.gz
else
touch updateinstallflag#{sc}#{nictype}
echo "Installing iperf3 jq"
apt -y install iperf3 jq
fi
fi

ping -c 4 #{ip + ((nictype + 1)*10).to_s}

echo "Doing iperf3 to ${title[#{nictype}]}, wait a little bit"
iperf3 -c #{ip + ((nictype + 1)*10).to_s} --json --logfile ${iperf3dir}output#{nictype}.json

if [[ "#{sc}" == "1" && "#{nictype}" == "0" ]]; then
echo "---" > ${iperf3dir}output.md
echo "title: Emulated nic vs virtio" >> ${iperf3dir}output.md
echo "description: VirtualBox Hypervisor" >> ${iperf3dir}output.md
echo "paginate: true" >> ${iperf3dir}output.md
echo "marp: true" >> ${iperf3dir}output.md
echo "theme: uncover" >> ${iperf3dir}output.md
echo "---" >> ${iperf3dir}output.md
echo "![bg](https://picsum.photos/720)" >> ${iperf3dir}output.md
echo "<!-- _class: invert -->" >> ${iperf3dir}output.md
echo "# Emulated nic vs virtio" >> ${iperf3dir}output.md
echo "## <span style='color:orange;'>$(date +"%Y-%m-%d")</span><!-- fit -->" >> ${iperf3dir}output.md
echo "## <span style='color:black;'>Let's see who's the winner is</span><!-- fit -->" >> ${iperf3dir}output.md
fi

echo "" >> ${iperf3dir}output.md
echo "---" >> ${iperf3dir}output.md
echo "<!-- _class: invert -->" >> ${iperf3dir}output.md
echo "${title[#{nictype}]}" >> ${iperf3dir}output.md
echo "* IP" >> ${iperf3dir}output.md
echo "    * Local: $(jq -r '.start.connected[].local_host' ${iperf3dir}output#{nictype}.json)" >> ${iperf3dir}output.md
echo "    * Remote: $(jq -r ".start.connected[].remote_host" ${iperf3dir}output#{nictype}.json)" >> ${iperf3dir}output.md
echo "* Bits/s" >> ${iperf3dir}output.md
echo "    * Sum sent: $(jq -r '.end.sum_sent.bits_per_second' ${iperf3dir}output#{nictype}.json | LC_ALL=C numfmt --to=iec-i)" >> ${iperf3dir}output.md
echo "    * Sum received: $(jq -r '.end.sum_received.bits_per_second' ${iperf3dir}output#{nictype}.json | LC_ALL=C numfmt --to=iec-i)" >> ${iperf3dir}output.md
echo "* Bytes" >> ${iperf3dir}output.md
echo "    * Sum sent: $(jq -r '.end.sum_sent.bytes' ${iperf3dir}output#{nictype}.json | LC_ALL=C numfmt --to=iec-i)" >> ${iperf3dir}output.md
echo "    * Sum received: $(jq -r '.end.sum_received.bytes' ${iperf3dir}output#{nictype}.json | LC_ALL=C numfmt --to=iec-i)" >> ${iperf3dir}output.md

if [[ "#{sc}" == "1" && "#{nictype}" == "1" ]]; then
echo "---" >> ${iperf3dir}output.md
echo "<!-- _class: invert -->" >> ${iperf3dir}output.md
echo "# Thanks!" >> ${iperf3dir}output.md
echo "* Teacher" >> ${iperf3dir}output.md
echo "    * For making this comes true" >> ${iperf3dir}output.md
echo "    * For letting us know to use this great tools" >> ${iperf3dir}output.md
echo "* HashiCorp" >> ${iperf3dir}output.md
echo "    * For using ruby as language to use when config Vagrant" >> ${iperf3dir}output.md
su - vagrant -c "./marp --pdf --html -I ${iperf3dir} -o ${iperf3dir}"
fi
systemctl poweroff
fi
SCRIPT
end
end
end
end
--- End code ---

GNUser:
I added polish and a minimal gui, then submitted this for the tc14 x86_64 repo. I considered calling it "ipp-print" but went with "easyprint" in the end. Bug reports are welcome.

Having to load cups + dbus + avahi (mDNS) to print to my LAN printer at a static ip address always struck me as cracking a nut with a sledgehammer. I'm glad that ipp makes it possible to crack the nut with a nutcracker :)

Note:
    I discovered the hard way that converting office documents (which are complex binary files and, depending on type, follow proprietary standards) into any other format is not a simple task. Therefore, for sake of minimalism, easyprint only prints pdf files out of the box.
   However, if you have the libreoffice extension or libreoffice appimage, then easyprint will automatically handle anything you throw at it--document files, image files, you name it. If you have the appimage, it should be somewhere in your PATH and you should change its name from LibreOffice_whatever.AppImage to simply libreoffice (or at least have a link in your PATH called libreoffice that points to the appimage).

Navigation

[0] Message Index

[*] Previous page

Go to full version