Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

save-html-as-image

gustavochavarria3.6kISC1.7.1

Save the html as image like png or jpg

convert html to image png or jpg, download html to image, dom to image, dom-to-image, download, file-saver, html-to-image, save dom, save html as image, save node, snapshot, screenshot

readme

save-html-as-image

npm version

This can be used for a couple of things, it can save HTML (DOM) as an image (JPG,PNG) and it can convert SVG images to PNG. Converting SVG to PNG is useful for Safari Browser compatibility since it cannot render SVG images.

Structure


saveAsPng(NODE_ELEMENT, USER_OPTIONS, DOM_OPTIONS);
saveAsJpeg(NODE_ELEMENT, USER_OPTIONS, DOM_OPTIONS);

Usage

import { saveAsPng, saveAsJpeg } from 'save-html-as-image';

const node = document.getElementById('elementId');

//download the node as png. Image (2019-12-01).png
saveAsPng(node);

//download the node as png. Report (2019-12-01).png
saveAsPng(node, { filename: 'Report', printDate: true });

//download the node as jpeg. Album.jpeg
saveAsJpeg(node, { filename: 'Album', printDate: false });

//download the node as jpeg. Album50.jpeg (With 50% of quality)
saveAsJpeg(node, 
  { filename: 'Album50', printDate: false }, 
  { quality: 0.5 }
);

//download the node as png and Add padding and center element
saveAsPng(
    node,
    {  filename: 'Report', printDate: true },
    {
      backgroundColor: 'rgba(101,198,185,0.5)',
      style: {
        padding: '4px',
        display: 'flex',
        justifyContent: 'center',
      },
    }
  );

USER Options

The options are:

  • filename : The name of the file when download.
  • printDate : The date of the download.
  • forceFixText : Prevent some error with text.

DOM Options (New)

Modify the DOM and IMAGE to download.

The options available are:

  • backgroundColor : A string value for the background color, any valid CSS color value.
  • width, height : Width and height in pixels to be applied to node before rendering. (Only work together)
  • style : An object whose properties to be copied to node's style before rendering. Check (https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Properties_Reference)
  • quality : A number between 0 and 1 indicating image quality (e.g. 0.92 => 92%) of the JPEG image. Defaults to 1.0
  • pixelRatio : The pixel ratio of the captured image. Default use the actual pixel ratio of the device. Defaults to 1

Hide Elements

You can hide elements adding the class "hide-when-downloading".

Hide elements apply display: hidden to the elements with this class;

  <section>
    <article>One..</article>
    <article class="hide-when-downloading">Two...</article>
  </section>
saveAsPng(node, {  filename: 'News', printDate: true });
  //Only the first article will be visible on the image

Note: The class "hide-when-downloading" must be on root element, That we want hide.

Remove Elements

You can remove elements adding the class "remove-when-downloading".

Remove Elements apply display: none to the elements. So the space into the element will remove.

Browsers

It's tested on the lasted Chrome (Chrome 76), Firefox and Safari.

We resolve the Safari support with a litte hack and replace svg with image. It's tested with FontAwesomeIcons svgs.

Dependency

We use a collection of packages to generate and donwnload images, all in one action. The packages are:

  • 'file-saver'
  • 'html-to-image'
  • 'save-svg-as-png'

How it work

  • This library use html-to-image library for convert the HTML (DOM) to Image (jpg and png).
  • We use file saver to download the image generated.
  • We use save-svg-as-png to convert the svgs to img elements, only on safary or browser with stricter secure on <foreignObject>

changelog

Changelog

All notable changes to this project will be documented in this file.

[1.6.0] - 2021-09-12

  • Fix bug with cache in HTML-TO-IMAGE package

[1.5.2] - 2021-03-22

  • Add 4px of default width & height

[1.5.1] - 2021-03-22

  • Fix Scale of images
  • NO Breaking changes

[1.5.0] - 2021-03-22

  • Adding DOM OPTIONS
  • Breaking changes

Breaking changes

  • Remove Default Padding in DOM, now you must provide the padding/margin to download
  • Remove Default Style (boxShadow: 'none')

[1.4.0] - 2021-02-25

  • Updating dependencies
  • No breaking changes

[1.3.3] - 2020-04-09

Fix

  • Add Remove-elements (display: none)

[1.3.2] - 2020-03-09

Fix

  • Add *Hide-elements (visibility: hidden)

[1.3.1] - 2020-02-26

Fix

  • Remove debug code (console.log)
  • Fix build (dependencies needed) ` These dependencies were not found:

    • file-saver in ./node_modules/save-html-as-image/dist/index.modern.js

      • save-svg-as-png in ./node_modules/save-html-as-image/dist/index.modern.js

      To install them, you can run: npm install --save file-saver save-svg-as-png `

[1.3.0] - 2020-02-26

Added

  • Hide elements with className "hide-when-downloading"

Refactors

  • Reduce the repetition of code

[1.2.1] - 2020-02-25

Fix

  • Crash when compiling code (beacause eslint rules);

[1.2.0] - 2019-10-11

Added

  • saveAsJpeg, now you can save the html as jpeg.

[1.1.0] - 2019-10-11

Added

  • Build project with microbundle (support for CommonJS/Node, JS Modules, unpkg).

[1.0.3] - 2019-10-10

Changed

  • downloadDOM was deprecated.

[1.0.0] - 2019-10-10

Added

  • Init project.