4

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!

4
  • Does it work if you open sample.html in Safari, create the webarchive from there, then move both files away and open the archive in Safari?
    – nohillside
    Aug 3, 2017 at 9:54
  • @patrix Good question! I've tried it and - as expected - Safari can do it: When Safari saves the archive, it is saved in a way that, when opened and rendered, the image shows up. - Strangely the file size of the Safari archive and the textutil archive are very similar, so I guess the textutil archive does contain the image, it just doesn't get referenced properly for rendering...
    – halloleo
    Aug 3, 2017 at 13:13
  • Well, it does indeed (you can check with plutil -p WEBARCHIVE). I didn't look into the differences though.
    – nohillside
    Aug 3, 2017 at 20:11
  • @patrix Interesting! Thx for the pointer to plutil (I knew that webarchives use the plist format, but didn't think of analysing them with plutil.Will investigate in more detail.
    – halloleo
    Aug 4, 2017 at 0:41

2 Answers 2

3

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.

-2

I think the commandline is not aware of any image being included.

I tested a simple webpage with image+text and opened it with Safari.

If you choose File -> Save as .. 'webarchive' you can see in the created webarchive that the image is appended at the end of the file.

It's quite obvious that Safari is using some Internal Swift/Objective-C function to implement the Image.

If you choose to get the html and have the image as a reference it seems to ignore the image and only output the text (via textutil).

I would not rely on webarchives when downloading webpages - instead use wget or curl or scrapers like scrapy/beautifulSoup and download them.

I know it's convenient to have 1 file, but that is not the best way to save images and such, that makes it pretty hard to extract them afterwards.

1
  • Thanks for looking at the question, but from my understanding your "answer" mainly states my problem: that "the commandline" i.e. textutil cannot create a proper webarchive - at least not how I call it. I am after a working tool/procedure for the command line to do exactly that.
    – halloleo
    Aug 3, 2017 at 12:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .