print.js 6.5 KB

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