code: def _export_as_raster(
      spec, path, scale: float, export_format: str
  ) -> Union[str, None]:
      if scale is None:
          scale = 2.0
  
      try:
          import cairosvg
      except ImportError:
          import sys
  
          print(
              "\n"
              "To export Lets-Plot figure to a PNG or PDF file please install CairoSVG library"
              "to your Python environment.\n"
              "CairoSVG is free and distributed under the LGPL-3.0 license.\n"
              "For more details visit: https://cairosvg.org/documentation/\n",
              file=sys.stderr,
          )
          return None
  
      if export_format.lower() == "png":
          export_function = cairosvg.svg2png
      elif export_format.lower() == "pdf":
          export_function = cairosvg.svg2pdf
      else:
          raise ValueError(
              "Unknown export format: {}".format(export_format)
          )
  
      from .. import _kbridge
  
      # Use SVG image-rendering style as Cairo doesn't support CSS image-rendering style,
      svg = _kbridge._generate_svg(
          spec.as_dict(), use_css_pixelated_image_rendering=False
      )
  
      if isinstance(path, str):
          abspath = _makedirs(path)
          result = abspath
      else:
          result = (
              None  # file-like object is provided. No path to return.
          )
      export_function(bytestring=svg, write_to=path, scale=scale)
      return result
  
imports: import io
  import json
  import os
  from typing import Union
  from lets_plot._global_settings import get_global_bool, has_global_value, FRAGMENTS_ENABLED
label_desc: Use image node_id 9 for calling a global function with example usage: _export_as_raster(spec, path, scale, export_format) and returns: result, None