Public
Edited
Jul 5, 2023
Insert cell
Insert cell
import { button } from "@jashkenas/inputs";
Insert cell

import domtoimage from 'dom-to-image';
Insert cell

import jsPDF from 'jspdf';

button("Download as PDF", async () => {
// Get the root element of the webpage
const rootElement = document.querySelector('body');

// Convert the root element to an image
const imageDataUrl = await domtoimage.toPng(rootElement);

// Create a new jsPDF instance
const pdf = new jsPDF();

// Add the image to the PDF document
pdf.addImage(imageDataUrl, 'PNG', 0, 0);

// Save the PDF document
pdf.save('webpage_snapshot.pdf');
});
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more