How to convert an HTML file with referenced images on the command line to a webarchive

command linefile conversionhtmlsafari

Under macOS I want to convert HTML files with referenced images on the command line/programatically to webarchives.

Given a sample file sample.html containing:

<html><body>
<h1>File with resource</h1>
<p>This file shows text and references an image.</p>
<img src="sample.jpg" />
</body></html>

and an image sample.jpg in the same directory, I can view the html file in any browser and it is render with the image. To convert to a webarchive I tried:

textutil -convert webarchive sample.html

but the resulting sample.webarchive does not, when opened in Safari, render the image – though it contains it (see comments).

How can I do the conversion from the command line correctly, so that the result contains and renders the image? After all that's what webarchives are for!

Best Answer

Use the tool webarchiver (which you can install via brew install webarchiver):

webarchiver -url sample.html -output sample.webarchive

The webarchive produced contains the image and Safari shows the image when it displays the webarchive.