How to Print Formatted HTML in Linux Server

How to print multiple line from variable in BASH to HTML output

Quick, ugly hack:

STORAGE=$(df -PTh | column -t | sort -n -k6n | sed 's:$:<br/>:')

This puts a line break at the end of every line ... but it won't format it as a table. For that, here's another quick, ugly hack:

STORAGE="<table>$(df -PTh | column -t | sort -n -k6n | sed 's:$:<br/>:;s:  *:</td><td>:;s:.*:<tr><td>&</td></tr>:')</table>"

This replaces all occurrances of one or more spaces with the necessary HTML to split columns, then adds table row wrappers to each line, and finally wraps the whole output in HTML <table> tags.

I say again: this is an ugly hack. It should work most of the time in a pinch, but will definitely break if you have anything else adding spaces, like a device name or filesystem name. (Ever mounted a USB stick with a space in its volume?)

Note that this ugly hack does not use anything specific to bash. You could do it just as easily in more traditional Bourne-style shells.

Another possibility, perhaps easier, would be simply to mark this text as being preformatted (which it is):

STORAGE="<pre>$(df -PTh | column -t | sort -n -k6n)</pre>"

Finally, if you want to get all bashy with this and use arrays, here's an example:

#!/usr/local/bin/bash

mapfile -t a_storage < <(df -PTh | column -t | sort -n -k6n)

td="th"
echo -n "<table>"
for line in "${a_storage[@]}"; do
a_line=($line)
echo "<tr>"
for field in ${a_line[@]}; do
echo " <$td> ${field//</<} </$td>"
done
echo -n " </tr>"
td="td"
done
echo "</table>"

This does pretty much exactly the same as the one-liners above, but it spells things out for you. It is still susceptible to unexpected whitespace.

Bash is not an ideal programming environment for generating HTML.

How to convert terminal output to HTML file format

(Already written as comment) You can use ssh to execute a command on the remote computer and process the output on the local computer. Example:

ssh user@aixhost 'ls -lrt /web/htdocs | tail -12' |
./ansi2html.sh --bg=dark >test.html

Standard input can be redirected, too, e.g:

ssh user1@host1 'cd frompath; tar -czf - sendme/' |
ssh user2@host2 'cd topath; tar -xzf -'

Note: I know nothing about Ansible, but I heard that with it you can do almost everything you could do without it.

Print html file with CUPS

Yes, there is.

Use this commandline:

lp -d printername -o document-format=text/html file.html

Update (in response to comments)

I provided an exact answer to the OP's question.

However, this (alone) does not guarantee that the file will be successfully printed. To achieve that, CUPS needs a filter which can process the input of MIME type text/html.

Such a filter is not provided by CUPS itself. However, it is easy to plug your own filter into the CUPS filtering system, and some Linux distributions ship such a filter capable to consume HTML files and convert them to a printable format.

You can check what happens in such a situation on your system. The cupsfilter command is a helper utility to run available/installed CUPS filters without the need to do actual printing through the CUPS daemon:

touch 1.html
/usr/sbin/cupsfilter --list-filters 1.html

Now on a system with no HTML consuming filter ready, you'd get this response:

cupsfilter: No filter to convert from text/html to application/pdf.

On a different system (like on a Mac), you'll see this:

xhtmltopdf

You can even force input and output MIME types to see which filters CUPS would run automatically when asked to print this file an a printer supporting that particular output MIME type (-i sets the input MIME type, -m the output):

/usr/sbin/cupsfilter                  \
-i text/html \
-m application/postscript \
--list-filters \
1.html
xhtmltopdf
cgpdftops

Here it would first convert HTML to PDF using xhtmltopdf, then transform the resulting PDF to PostScript using cgpdftops.

If you skip the --list-filters parameter, the command would actually even go ahead and do the conversion by actively running (not just listing) the two filters and emit the result to <stdout>.

You could write your own CUPS filter based on a Shell script. The only other ingredient you need is a command line tool, such as htmldoc or wkhtmltopdf, which is able to process HTML input and produce some format that in turn could be consumed by the CUPS filtering chain further down the road.

Be aware, that some (especially JavaScript-heavy) HTML files cannot be successfully processed by simple command line tools into print-ready formats.

If you need more details about this, just ask another question...

Sending HTML mail using a shell script

First you need to compose the message. The bare minimum is composed of these two headers:

MIME-Version: 1.0
Content-Type: text/html

... and the appropriate message body:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>

<p>Hello, world!</p>

</body>
</html>

Once you have it, you can pass the appropriate information to the mail command:

body = '...'

echo $body | mail \
-a "From: me@example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "This is the subject" \
you@example.com

This is an oversimplified example, since you also need to take care of charsets, encodings, maximum line length... But this is basically the idea.

Alternatively, you can write your script in Perl or PHP rather than plain shell.

Update

A shell script is basically a text file with Unix line endings that starts with a line called shebang that tells the shell what interpreter it must pass the file to, follow some commands in the language the interpreter understands and has execution permission (in Unix that's a file attribute). E.g., let's say you save the following as hello-world:

#!/bin/sh

echo Hello, world!

Then you assign execution permission:

chmod +x hello-world

And you can finally run it:

./hello-world

Whatever, this is kind of unrelated to the original question. You should get familiar with basic shell scripting before doing advanced tasks with it. Here you are a couple of links about bash, a popular shell:

http://www.gnu.org/software/bash/manual/html_node/index.html

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html



Related Topics



Leave a reply



Submit