blob: 4214519ff0730f05017c74238afe172a0c632c17 [file] [log] [blame]
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001import { SLIDES_SELECTOR } from '../utils/constants.js'
2import { queryAll, createStyleSheet } from '../utils/util.js'
3
4/**
5 * Setups up our presentation for printing/exporting to PDF.
6 */
7export default class Print {
8
9 constructor( Reveal ) {
10
11 this.Reveal = Reveal;
12
13 }
14
15 /**
16 * Configures the presentation for printing to a static
17 * PDF.
18 */
19 async setupPDF() {
20
21 const config = this.Reveal.getConfig();
22 const slides = queryAll( this.Reveal.getRevealElement(), SLIDES_SELECTOR )
23
24 // Compute slide numbers now, before we start duplicating slides
Marc Kupietz09b75752023-10-07 09:32:19 +020025 const injectPageNumbers = config.slideNumber && /all|print/i.test( config.showSlideNumber );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020026
27 const slideSize = this.Reveal.getComputedSlideSize( window.innerWidth, window.innerHeight );
28
29 // Dimensions of the PDF pages
30 const pageWidth = Math.floor( slideSize.width * ( 1 + config.margin ) ),
31 pageHeight = Math.floor( slideSize.height * ( 1 + config.margin ) );
32
33 // Dimensions of slides within the pages
34 const slideWidth = slideSize.width,
35 slideHeight = slideSize.height;
36
37 await new Promise( requestAnimationFrame );
38
39 // Let the browser know what page size we want to print
40 createStyleSheet( '@page{size:'+ pageWidth +'px '+ pageHeight +'px; margin: 0px;}' );
41
42 // Limit the size of certain elements to the dimensions of the slide
43 createStyleSheet( '.reveal section>img, .reveal section>video, .reveal section>iframe{max-width: '+ slideWidth +'px; max-height:'+ slideHeight +'px}' );
44
45 document.documentElement.classList.add( 'print-pdf' );
46 document.body.style.width = pageWidth + 'px';
47 document.body.style.height = pageHeight + 'px';
48
Christophe Dervieux8afae132021-12-06 15:16:42 +010049 const viewportElement = document.querySelector( '.reveal-viewport' );
50 let presentationBackground;
51 if( viewportElement ) {
52 const viewportStyles = window.getComputedStyle( viewportElement );
53 if( viewportStyles && viewportStyles.background ) {
54 presentationBackground = viewportStyles.background;
55 }
56 }
57
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020058 // Make sure stretch elements fit on slide
59 await new Promise( requestAnimationFrame );
60 this.Reveal.layoutSlideContents( slideWidth, slideHeight );
61
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020062 // Batch scrollHeight access to prevent layout thrashing
63 await new Promise( requestAnimationFrame );
64
65 const slideScrollHeights = slides.map( slide => slide.scrollHeight );
66
67 const pages = [];
68 const pageContainer = slides[0].parentNode;
Marc Kupietz09b75752023-10-07 09:32:19 +020069 let slideNumber = 1;
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020070
71 // Slide and slide background layout
72 slides.forEach( function( slide, index ) {
73
74 // Vertical stacks are not centred since their section
75 // children will be
76 if( slide.classList.contains( 'stack' ) === false ) {
77 // Center the slide inside of the page, giving the slide some margin
78 let left = ( pageWidth - slideWidth ) / 2;
79 let top = ( pageHeight - slideHeight ) / 2;
80
81 const contentHeight = slideScrollHeights[ index ];
82 let numberOfPages = Math.max( Math.ceil( contentHeight / pageHeight ), 1 );
83
84 // Adhere to configured pages per slide limit
85 numberOfPages = Math.min( numberOfPages, config.pdfMaxPagesPerSlide );
86
87 // Center slides vertically
88 if( numberOfPages === 1 && config.center || slide.classList.contains( 'center' ) ) {
89 top = Math.max( ( pageHeight - contentHeight ) / 2, 0 );
90 }
91
92 // Wrap the slide in a page element and hide its overflow
93 // so that no page ever flows onto another
94 const page = document.createElement( 'div' );
95 pages.push( page );
96
97 page.className = 'pdf-page';
98 page.style.height = ( ( pageHeight + config.pdfPageHeightOffset ) * numberOfPages ) + 'px';
Christophe Dervieux8afae132021-12-06 15:16:42 +010099
100 // Copy the presentation-wide background to each individual
101 // page when printing
102 if( presentationBackground ) {
103 page.style.background = presentationBackground;
104 }
105
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200106 page.appendChild( slide );
107
108 // Position the slide inside of the page
109 slide.style.left = left + 'px';
110 slide.style.top = top + 'px';
111 slide.style.width = slideWidth + 'px';
112
Marc Kupietz09b75752023-10-07 09:32:19 +0200113 this.Reveal.slideContent.layout( slide );
Christophe Dervieux8afae132021-12-06 15:16:42 +0100114
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200115 if( slide.slideBackgroundElement ) {
116 page.insertBefore( slide.slideBackgroundElement, slide );
117 }
118
119 // Inject notes if `showNotes` is enabled
120 if( config.showNotes ) {
121
122 // Are there notes for this slide?
123 const notes = this.Reveal.getSlideNotes( slide );
124 if( notes ) {
125
126 const notesSpacing = 8;
127 const notesLayout = typeof config.showNotes === 'string' ? config.showNotes : 'inline';
128 const notesElement = document.createElement( 'div' );
129 notesElement.classList.add( 'speaker-notes' );
130 notesElement.classList.add( 'speaker-notes-pdf' );
131 notesElement.setAttribute( 'data-layout', notesLayout );
132 notesElement.innerHTML = notes;
133
134 if( notesLayout === 'separate-page' ) {
135 pages.push( notesElement );
136 }
137 else {
138 notesElement.style.left = notesSpacing + 'px';
139 notesElement.style.bottom = notesSpacing + 'px';
140 notesElement.style.width = ( pageWidth - notesSpacing*2 ) + 'px';
141 page.appendChild( notesElement );
142 }
143
144 }
145
146 }
147
Marc Kupietz09b75752023-10-07 09:32:19 +0200148 // Inject page numbers if `slideNumbers` are enabled
149 if( injectPageNumbers ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200150 const numberElement = document.createElement( 'div' );
151 numberElement.classList.add( 'slide-number' );
152 numberElement.classList.add( 'slide-number-pdf' );
Marc Kupietz09b75752023-10-07 09:32:19 +0200153 numberElement.innerHTML = slideNumber++;
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200154 page.appendChild( numberElement );
155 }
156
157 // Copy page and show fragments one after another
158 if( config.pdfSeparateFragments ) {
159
160 // Each fragment 'group' is an array containing one or more
161 // fragments. Multiple fragments that appear at the same time
162 // are part of the same group.
163 const fragmentGroups = this.Reveal.fragments.sort( page.querySelectorAll( '.fragment' ), true );
164
165 let previousFragmentStep;
166
Marc Kupietz09b75752023-10-07 09:32:19 +0200167 fragmentGroups.forEach( function( fragments, index ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200168
169 // Remove 'current-fragment' from the previous group
170 if( previousFragmentStep ) {
171 previousFragmentStep.forEach( function( fragment ) {
172 fragment.classList.remove( 'current-fragment' );
173 } );
174 }
175
176 // Show the fragments for the current index
177 fragments.forEach( function( fragment ) {
178 fragment.classList.add( 'visible', 'current-fragment' );
179 }, this );
180
181 // Create a separate page for the current fragment state
182 const clonedPage = page.cloneNode( true );
Marc Kupietz09b75752023-10-07 09:32:19 +0200183
184 // Inject unique page numbers for fragments
185 if( injectPageNumbers ) {
186 const numberElement = clonedPage.querySelector( '.slide-number-pdf' );
187 const fragmentNumber = index + 1;
188 numberElement.innerHTML += '.' + fragmentNumber;
189 }
190
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200191 pages.push( clonedPage );
192
193 previousFragmentStep = fragments;
194
195 }, this );
196
197 // Reset the first/original page so that all fragments are hidden
198 fragmentGroups.forEach( function( fragments ) {
199 fragments.forEach( function( fragment ) {
200 fragment.classList.remove( 'visible', 'current-fragment' );
201 } );
202 } );
203
204 }
205 // Show all fragments
206 else {
207 queryAll( page, '.fragment:not(.fade-out)' ).forEach( function( fragment ) {
208 fragment.classList.add( 'visible' );
209 } );
210 }
211
212 }
213
214 }, this );
215
216 await new Promise( requestAnimationFrame );
217
218 pages.forEach( page => pageContainer.appendChild( page ) );
219
Marc Kupietz09b75752023-10-07 09:32:19 +0200220 // Re-run JS-based content layout after the slide is added to page DOM
221 this.Reveal.slideContent.layout( this.Reveal.getSlidesElement() );
222
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200223 // Notify subscribers that the PDF layout is good to go
224 this.Reveal.dispatchEvent({ type: 'pdf-ready' });
225
226 }
227
228 /**
229 * Checks if this instance is being used to print a PDF.
230 */
231 isPrintingPDF() {
232
233 return ( /print-pdf/gi ).test( window.location.search );
234
235 }
236
237}