print.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { SLIDES_SELECTOR } from '../utils/constants.js'
  2. import { queryAll, createStyleSheet } from '../utils/util.js'
  3. /**
  4. * Setups up our presentation for printing/exporting to PDF.
  5. */
  6. export default class Print {
  7. constructor( Reveal ) {
  8. this.Reveal = Reveal;
  9. }
  10. /**
  11. * Configures the presentation for printing to a static
  12. * PDF.
  13. */
  14. async setupPDF() {
  15. const config = this.Reveal.getConfig();
  16. const slides = queryAll( this.Reveal.getRevealElement(), SLIDES_SELECTOR )
  17. // Compute slide numbers now, before we start duplicating slides
  18. const doingSlideNumbers = config.slideNumber && /all|print/i.test( config.showSlideNumber );
  19. const slideSize = this.Reveal.getComputedSlideSize( window.innerWidth, window.innerHeight );
  20. // Dimensions of the PDF pages
  21. const pageWidth = Math.floor( slideSize.width * ( 1 + config.margin ) ),
  22. pageHeight = Math.floor( slideSize.height * ( 1 + config.margin ) );
  23. // Dimensions of slides within the pages
  24. const slideWidth = slideSize.width,
  25. slideHeight = slideSize.height;
  26. await new Promise( requestAnimationFrame );
  27. // Let the browser know what page size we want to print
  28. createStyleSheet( '@page{size:'+ pageWidth +'px '+ pageHeight +'px; margin: 0px;}' );
  29. // Limit the size of certain elements to the dimensions of the slide
  30. createStyleSheet( '.reveal section>img, .reveal section>video, .reveal section>iframe{max-width: '+ slideWidth +'px; max-height:'+ slideHeight +'px}' );
  31. document.documentElement.classList.add( 'print-pdf' );
  32. document.body.style.width = pageWidth + 'px';
  33. document.body.style.height = pageHeight + 'px';
  34. const viewportElement = document.querySelector( '.reveal-viewport' );
  35. let presentationBackground;
  36. if( viewportElement ) {
  37. const viewportStyles = window.getComputedStyle( viewportElement );
  38. if( viewportStyles && viewportStyles.background ) {
  39. presentationBackground = viewportStyles.background;
  40. }
  41. }
  42. // Make sure stretch elements fit on slide
  43. await new Promise( requestAnimationFrame );
  44. this.Reveal.layoutSlideContents( slideWidth, slideHeight );
  45. // Batch scrollHeight access to prevent layout thrashing
  46. await new Promise( requestAnimationFrame );
  47. const slideScrollHeights = slides.map( slide => slide.scrollHeight );
  48. const pages = [];
  49. const pageContainer = slides[0].parentNode;
  50. // Slide and slide background layout
  51. slides.forEach( function( slide, index ) {
  52. // Vertical stacks are not centred since their section
  53. // children will be
  54. if( slide.classList.contains( 'stack' ) === false ) {
  55. // Center the slide inside of the page, giving the slide some margin
  56. let left = ( pageWidth - slideWidth ) / 2;
  57. let top = ( pageHeight - slideHeight ) / 2;
  58. const contentHeight = slideScrollHeights[ index ];
  59. let numberOfPages = Math.max( Math.ceil( contentHeight / pageHeight ), 1 );
  60. // Adhere to configured pages per slide limit
  61. numberOfPages = Math.min( numberOfPages, config.pdfMaxPagesPerSlide );
  62. // Center slides vertically
  63. if( numberOfPages === 1 && config.center || slide.classList.contains( 'center' ) ) {
  64. top = Math.max( ( pageHeight - contentHeight ) / 2, 0 );
  65. }
  66. // Wrap the slide in a page element and hide its overflow
  67. // so that no page ever flows onto another
  68. const page = document.createElement( 'div' );
  69. pages.push( page );
  70. page.className = 'pdf-page';
  71. page.style.height = ( ( pageHeight + config.pdfPageHeightOffset ) * numberOfPages ) + 'px';
  72. // Copy the presentation-wide background to each individual
  73. // page when printing
  74. if( presentationBackground ) {
  75. page.style.background = presentationBackground;
  76. }
  77. page.appendChild( slide );
  78. // Position the slide inside of the page
  79. slide.style.left = left + 'px';
  80. slide.style.top = top + 'px';
  81. slide.style.width = slideWidth + 'px';
  82. // Re-run the slide layout so that r-fit-text is applied based on
  83. // the printed slide size
  84. this.Reveal.slideContent.layout( slide )
  85. if( slide.slideBackgroundElement ) {
  86. page.insertBefore( slide.slideBackgroundElement, slide );
  87. }
  88. // Inject notes if `showNotes` is enabled
  89. if( config.showNotes ) {
  90. // Are there notes for this slide?
  91. const notes = this.Reveal.getSlideNotes( slide );
  92. if( notes ) {
  93. const notesSpacing = 8;
  94. const notesLayout = typeof config.showNotes === 'string' ? config.showNotes : 'inline';
  95. const notesElement = document.createElement( 'div' );
  96. notesElement.classList.add( 'speaker-notes' );
  97. notesElement.classList.add( 'speaker-notes-pdf' );
  98. notesElement.setAttribute( 'data-layout', notesLayout );
  99. notesElement.innerHTML = notes;
  100. if( notesLayout === 'separate-page' ) {
  101. pages.push( notesElement );
  102. }
  103. else {
  104. notesElement.style.left = notesSpacing + 'px';
  105. notesElement.style.bottom = notesSpacing + 'px';
  106. notesElement.style.width = ( pageWidth - notesSpacing*2 ) + 'px';
  107. page.appendChild( notesElement );
  108. }
  109. }
  110. }
  111. // Inject slide numbers if `slideNumbers` are enabled
  112. if( doingSlideNumbers ) {
  113. const slideNumber = index + 1;
  114. const numberElement = document.createElement( 'div' );
  115. numberElement.classList.add( 'slide-number' );
  116. numberElement.classList.add( 'slide-number-pdf' );
  117. numberElement.innerHTML = slideNumber;
  118. page.appendChild( numberElement );
  119. }
  120. // Copy page and show fragments one after another
  121. if( config.pdfSeparateFragments ) {
  122. // Each fragment 'group' is an array containing one or more
  123. // fragments. Multiple fragments that appear at the same time
  124. // are part of the same group.
  125. const fragmentGroups = this.Reveal.fragments.sort( page.querySelectorAll( '.fragment' ), true );
  126. let previousFragmentStep;
  127. fragmentGroups.forEach( function( fragments ) {
  128. // Remove 'current-fragment' from the previous group
  129. if( previousFragmentStep ) {
  130. previousFragmentStep.forEach( function( fragment ) {
  131. fragment.classList.remove( 'current-fragment' );
  132. } );
  133. }
  134. // Show the fragments for the current index
  135. fragments.forEach( function( fragment ) {
  136. fragment.classList.add( 'visible', 'current-fragment' );
  137. }, this );
  138. // Create a separate page for the current fragment state
  139. const clonedPage = page.cloneNode( true );
  140. pages.push( clonedPage );
  141. previousFragmentStep = fragments;
  142. }, this );
  143. // Reset the first/original page so that all fragments are hidden
  144. fragmentGroups.forEach( function( fragments ) {
  145. fragments.forEach( function( fragment ) {
  146. fragment.classList.remove( 'visible', 'current-fragment' );
  147. } );
  148. } );
  149. }
  150. // Show all fragments
  151. else {
  152. queryAll( page, '.fragment:not(.fade-out)' ).forEach( function( fragment ) {
  153. fragment.classList.add( 'visible' );
  154. } );
  155. }
  156. }
  157. }, this );
  158. await new Promise( requestAnimationFrame );
  159. pages.forEach( page => pageContainer.appendChild( page ) );
  160. // Notify subscribers that the PDF layout is good to go
  161. this.Reveal.dispatchEvent({ type: 'pdf-ready' });
  162. }
  163. /**
  164. * Checks if this instance is being used to print a PDF.
  165. */
  166. isPrintingPDF() {
  167. return ( /print-pdf/gi ).test( window.location.search );
  168. }
  169. }