pdf.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>PDF 预览</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. font-family: system-ui, sans-serif;
  13. }
  14. .container {
  15. width: 100%;
  16. position: relative;
  17. }
  18. #pdf-container {
  19. display: flex;
  20. flex-direction: column;
  21. align-items: center;
  22. gap: 15px;
  23. min-height: 300px;
  24. }
  25. .pdf-page {
  26. display: block;
  27. width: 100%;
  28. height: auto;
  29. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  30. border-radius: 4px;
  31. }
  32. .tip {
  33. color: #999;
  34. font-size: 18px;
  35. padding: 100px 20px;
  36. text-align: center;
  37. }
  38. /* ========================
  39. 完全还原 uni-loading 4点跳动
  40. ======================== */
  41. .loading-container {
  42. position: fixed;
  43. top: 0;
  44. left: 0;
  45. width: 100vw;
  46. height: 100vh;
  47. background: #fff;
  48. display: flex;
  49. align-items: center;
  50. justify-content: center;
  51. z-index: 99999;
  52. transition: opacity 0.3s ease;
  53. }
  54. .loading2 {
  55. width: 30px;
  56. height: 30px;
  57. position: relative;
  58. -webkit-transform: rotate(10deg);
  59. transform: rotate(10deg);
  60. }
  61. .loading2 .shape {
  62. border-radius: 5px;
  63. }
  64. .loading2 {
  65. -webkit-animation: rotation 1s infinite;
  66. animation: rotation 1s infinite;
  67. }
  68. .shape {
  69. position: absolute;
  70. width: 10px;
  71. height: 10px;
  72. border-radius: 1px;
  73. }
  74. .shape.shape1 { left: 0; background-color: #1890FF; }
  75. .shape.shape2 { right: 0; background-color: #91CB74; }
  76. .shape.shape3 { bottom: 0; background-color: #FAC858; }
  77. .shape.shape4 { bottom: 0; right: 0; background-color: #EE6666; }
  78. .loading2 .shape1 {
  79. -webkit-animation: animation2shape1 0.5s ease 0s infinite alternate;
  80. animation: animation2shape1 0.5s ease 0s infinite alternate;
  81. }
  82. @-webkit-keyframes animation2shape1 {
  83. from { -webkit-transform: translate(0, 0); transform: translate(0, 0); }
  84. to { -webkit-transform: translate(20px, 20px); transform: translate(20px, 20px); }
  85. }
  86. @keyframes animation2shape1 {
  87. from { -webkit-transform: translate(0, 0); transform: translate(0, 0); }
  88. to { -webkit-transform: translate(20px, 20px); transform: translate(20px, 20px); }
  89. }
  90. .loading2 .shape2 {
  91. -webkit-animation: animation2shape2 0.5s ease 0s infinite alternate;
  92. animation: animation2shape2 0.5s ease 0s infinite alternate;
  93. }
  94. @-webkit-keyframes animation2shape2 {
  95. from { -webkit-transform: translate(0, 0); transform: translate(0, 0); }
  96. to { -webkit-transform: translate(-20px, 20px); transform: translate(-20px, 20px); }
  97. }
  98. @keyframes animation2shape2 {
  99. from { -webkit-transform: translate(0, 0); transform: translate(0, 0); }
  100. to { -webkit-transform: translate(-20px, 20px); transform: translate(-20px, 20px); }
  101. }
  102. .loading2 .shape3 {
  103. -webkit-animation: animation2shape3 0.5s ease 0s infinite alternate;
  104. animation: animation2shape3 0.5s ease 0s infinite alternate;
  105. }
  106. @-webkit-keyframes animation2shape3 {
  107. from { -webkit-transform: translate(0, 0); transform: translate(0, 0); }
  108. to { -webkit-transform: translate(20px, -20px); transform: translate(20px, -20px); }
  109. }
  110. @keyframes animation2shape3 {
  111. from { -webkit-transform: translate(0, 0); transform: translate(0, 0); }
  112. to { -webkit-transform: translate(20px, -20px); transform: translate(20px, -20px); }
  113. }
  114. .loading2 .shape4 {
  115. -webkit-animation: animation2shape4 0.5s ease 0s infinite alternate;
  116. animation: animation2shape4 0.5s ease 0s infinite alternate;
  117. }
  118. @-webkit-keyframes animation2shape4 {
  119. from { -webkit-transform: translate(0, 0); transform: translate(0, 0); }
  120. to { -webkit-transform: translate(-20px, -20px); transform: translate(-20px, -20px); }
  121. }
  122. @keyframes animation2shape4 {
  123. from { -webkit-transform: translate(0, 0); transform: translate(0, 0); }
  124. to { -webkit-transform: translate(-20px, -20px); transform: translate(-20px, -20px); }
  125. }
  126. @-webkit-keyframes rotation {
  127. 0% { transform: rotate(0deg); }
  128. 100% { transform: rotate(360deg); }
  129. }
  130. @keyframes rotation {
  131. 0% { transform: rotate(0deg); }
  132. 100% { transform: rotate(360deg); }
  133. }
  134. </style>
  135. </head>
  136. <body>
  137. <!-- loading -->
  138. <div class="loading-container" id="loading">
  139. <div class="loading2">
  140. <div class="shape shape1"></div>
  141. <div class="shape shape2"></div>
  142. <div class="shape shape3"></div>
  143. <div class="shape shape4"></div>
  144. </div>
  145. </div>
  146. <div class="container">
  147. <div id="pdf-container"></div>
  148. </div>
  149. <script src="js/pdf.min.js"></script>
  150. <script>
  151. pdfjsLib.GlobalWorkerOptions.workerSrc = 'js/pdf.worker.min.js'
  152. const container = document.getElementById('pdf-container')
  153. let pdfDoc = null
  154. // ✅ 立即隐藏 loading(无卡顿)
  155. function hideLoading() {
  156. const el = document.getElementById('loading')
  157. if (el) {
  158. el.style.opacity = '0'
  159. setTimeout(() => {
  160. el.style.display = 'none'
  161. }, 300)
  162. }
  163. }
  164. // 获取URL参数
  165. function getPdfUrl() {
  166. const params = new URLSearchParams(window.location.search)
  167. return params.get('pdf')
  168. }
  169. // XHR 加载解决跨域
  170. function loadPdfAsBlob(url) {
  171. return new Promise((resolve, reject) => {
  172. const xhr = new XMLHttpRequest()
  173. xhr.open('GET', url)
  174. xhr.responseType = 'blob'
  175. xhr.onload = () => {
  176. if (xhr.status >= 200 && xhr.status < 300) resolve(xhr.response)
  177. else reject(new Error('加载失败'))
  178. }
  179. xhr.onerror = reject
  180. xhr.send()
  181. })
  182. }
  183. // 高清渲染 + 宽度100%自适应
  184. async function renderAllPages() {
  185. container.innerHTML = ''
  186. const containerWidth = container.clientWidth - 20
  187. for (let i = 1; i <= pdfDoc.numPages; i++) {
  188. const page = await pdfDoc.getPage(i)
  189. const defaultViewport = page.getViewport({ scale: 1 })
  190. const scale = containerWidth / defaultViewport.width
  191. const viewport = page.getViewport({ scale })
  192. const canvas = document.createElement('canvas')
  193. const ctx = canvas.getContext('2d')
  194. const dpr = window.devicePixelRatio || 1
  195. canvas.width = viewport.width * dpr
  196. canvas.height = viewport.height * dpr
  197. canvas.style.width = "100%"
  198. canvas.style.height = "auto"
  199. ctx.scale(dpr, dpr)
  200. canvas.className = 'pdf-page'
  201. await page.render({ canvasContext: ctx, viewport }).promise
  202. container.appendChild(canvas)
  203. }
  204. }
  205. // 初始化
  206. window.onload = async function () {
  207. const pdfUrl = getPdfUrl()
  208. if (!pdfUrl) {
  209. container.innerHTML = '<div class="tip"></div>'
  210. hideLoading()
  211. return
  212. }
  213. try {
  214. const blob = await loadPdfAsBlob(pdfUrl)
  215. const blobUrl = URL.createObjectURL(blob)
  216. // ✅ PDF 加载完成 立即隐藏 loading
  217. pdfDoc = await pdfjsLib.getDocument(blobUrl).promise
  218. hideLoading() // 👈 这里提前关,绝不卡顿
  219. await renderAllPages()
  220. } catch (e) {
  221. container.innerHTML = '<div class="tip">PDF 加载失败</div>'
  222. hideLoading()
  223. console.error(e)
  224. }
  225. }
  226. </script>
  227. </body>
  228. </html>