import { ChangeDetectionStrategy, Component, computed, effect, inject, signal, ViewEncapsulation, } from '@angular/core'; import { copyImage, EpdfDocumentGate, provideEmbedPdf, saveFile } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRender, EpdfRenderLayer, EpdfSnapshotLayer, withRender, } from '@embedpdf/angular/render'; import { EpdfInteraction, withInteraction } from '@embedpdf/angular/interaction'; import { localEngine } from '@embedpdf/engine'; // [!doc-source welcome] const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSnapshotLayer], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withInteraction({ defaultTool: 'pan' }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './snapshot.css', encapsulation: ViewEncapsulation.None, template: `
{{ readout() }}

Loading…

`, }) export class App { protected readonly interaction = inject(EpdfInteraction); private readonly render = inject(EpdfRender); protected readonly snapshot = computed(() => this.render.getSnapshot()); private readonly said = signal(''); protected readonly readout = computed( () => this.said() || (this.interaction.activeToolId() === 'snapshot' && !this.snapshot() ? 'Drag a box on a page' : ''), ); constructor() { // A new snapshot, or none: nothing copied yet. effect(() => { this.snapshot(); this.said.set(''); }); } protected copy() { const snapshot = this.snapshot(); if (!snapshot) return; // In the click itself: the picture goes to the clipboard as it comes. copyImage(snapshot.image).then( () => this.said.set('Copied: paste it anywhere'), () => this.said.set('This browser didn’t allow the copy'), ); } protected async save() { const snapshot = this.snapshot(); if (!snapshot) return; saveFile(await (await snapshot.image).blob(), 'snapshot.png'); } }