viewer.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <!DOCTYPE html>
  2. <html dir="ltr" mozdisallowselectionprint moznomarginboxes>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
  5. <meta name="apple-mobile-web-app-capable" content="yes">
  6. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  7. <title>PDF</title>
  8. <link rel="stylesheet" href="../build/reset.css">
  9. <link rel="resource" type="application/l10n" href="locale/locale.properties">
  10. <script src="../build/pdf.js"></script>
  11. <style type="text/css">
  12. html,
  13. body,
  14. .mui-content {
  15. padding: 0;
  16. margin: 0;
  17. height: 100%;
  18. box-sizing: border-box;
  19. padding-bottom: 0 !important;
  20. }
  21. .mui-content {
  22. display: flex;
  23. flex-direction: row;
  24. overflow: hidden;
  25. }
  26. .mui-content .side-menu {
  27. width: 100px;
  28. height: 100%;
  29. overflow-y: auto;
  30. padding: 10px;
  31. border-right: 1px solid #e6e6e6;
  32. }
  33. .mui-content .side-menu .viewer-image {
  34. border: 1px solid #e6e6e6;
  35. background: #fff;
  36. margin-bottom: 10px;
  37. border-radius: 5px;
  38. overflow: hidden;
  39. }
  40. .mui-content img {
  41. width: 100%;
  42. height: 100%;
  43. }
  44. .mui-content .side-menu .viewer-image.active {
  45. border-color: #4681e9;
  46. }
  47. .mui-content .view-box {
  48. flex: 1;
  49. width: 0;
  50. height: 100%;
  51. overflow-y: auto;
  52. }
  53. .mui-content .view-box .page {
  54. text-align: center;
  55. display: none;
  56. position: relative;
  57. overflow: hidden;
  58. }
  59. .mui-content .view-box .page.active {
  60. display: block;
  61. }
  62. .mui-content .view-box .page .seal {
  63. position: absolute;
  64. width: 30px;
  65. height: 30px;
  66. top: 0;
  67. left: 0;
  68. z-index: 2;
  69. }
  70. .mui-content .view-box .page .sign {
  71. position: absolute;
  72. width: 30px;
  73. height: 10px;
  74. top: 0;
  75. left: 0;
  76. z-index: 2;
  77. }
  78. .btn-box {
  79. text-align: center;
  80. margin: 20px 0;
  81. }
  82. .btn {
  83. font-size: 12px;
  84. padding: 5px 10px;
  85. border-radius: 5px;
  86. margin: 0 5px;
  87. }
  88. #operation-menu {
  89. display: none;
  90. }
  91. .sure {
  92. top: 0;
  93. right: 0;
  94. padding: 15px;
  95. }
  96. /*字体大小*/
  97. .size-24 {
  98. font-size: 24px;
  99. }
  100. .size-18 {
  101. font-size: 18px;
  102. }
  103. .size-16 {
  104. font-size: 16px;
  105. }
  106. .size-14 {
  107. font-size: 14px;
  108. }
  109. .size-12 {
  110. font-size: 12px;
  111. }
  112. .size-10 {
  113. font-size: 10px;
  114. }
  115. .size-weight {
  116. font-weight: bold;
  117. }
  118. .red-color {
  119. color: #fd5c54;
  120. }
  121. .blue-color {
  122. color: #5c88ef;
  123. }
  124. .gray-color {
  125. color: #666666;
  126. }
  127. .grays-color {
  128. color: #999999;
  129. }
  130. .orange-color {
  131. color: #f8a722;
  132. }
  133. .white-color {
  134. color: #fff;
  135. }
  136. .backgroud-blue {
  137. background: #5c88ef;
  138. }
  139. .backgroud-red {
  140. background: #fd5c54;
  141. }
  142. .backgroud-yellow {
  143. background: #f8a722;
  144. }
  145. .backgroud-qing {
  146. background: #5cc8b4;
  147. }
  148. .backgroud-white {
  149. background: #fff;
  150. }
  151. .displayNone {
  152. display: none;
  153. }
  154. </style>
  155. </head>
  156. <body>
  157. <div class="mui-content">
  158. <div id="viewer-image" class="side-menu"></div>
  159. <div class="view-box">
  160. <div id="viewer"></div>
  161. </div>
  162. </div>
  163. <script type="text/javascript">
  164. PDFJS.workerSrc = '../build/pdf.worker.js';
  165. var desiredWidth = document.getElementsByClassName('view-box')[0].clientWidth;
  166. var pageIndex = 0,
  167. imgIndex = 0,
  168. nowIndex = 0,
  169. moveType, url, positionList = [];
  170. function getQueryObject(url) {
  171. url = url == null ? window.location.href : url
  172. const search = url.substring(url.lastIndexOf('?') + 1)
  173. const obj = {}
  174. const reg = /([^?&=]+)=([^?&=]*)/g
  175. search.replace(reg, (rs, $1, $2) => {
  176. const name = decodeURIComponent($1)
  177. let val = decodeURIComponent($2)
  178. val = String(val)
  179. obj[name] = val
  180. return rs
  181. })
  182. return obj
  183. }
  184. var query = getQueryObject();
  185. if (query.type == 'look') {
  186. positionList = window.parent['getPositionList']();
  187. } else if (query.type == 'update') {
  188. // document.getElementsByClassName('operation')[0].style.display = 'block';
  189. positionList = window.parent['getPositionList']();
  190. } else {
  191. // document.getElementsByClassName('operation')[0].style.display = 'block';
  192. }
  193. function renderPage(page, i) {
  194. var viewport = page.getViewport(1);
  195. var scale = (desiredWidth - 20) / viewport.width;
  196. var scaledViewport = page.getViewport(scale);
  197. var outputScale = window.devicePixelRatio || 1;
  198. var canvas = document.getElementById('the-canvas' + pageIndex);
  199. pageIndex++
  200. var context = canvas.getContext('2d');
  201. canvas.width = Math.floor(scaledViewport.width * outputScale);
  202. canvas.height = Math.floor(scaledViewport.height * outputScale);
  203. canvas.style.width = Math.floor(scaledViewport.width) + "px";
  204. canvas.style.height = Math.floor(scaledViewport.height) + "px";
  205. var transform = outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : null;
  206. var renderContext = {
  207. canvasContext: context,
  208. transform: transform,
  209. viewport: scaledViewport
  210. };
  211. page.render(renderContext).then(function() {
  212. var base64 = canvas.toDataURL('image/jpeg', 0.7);
  213. var div = document.createElement('div');
  214. div.className = 'viewer-image';
  215. div.id = imgIndex;
  216. div.onclick = function() {
  217. if (this.className == 'viewer-image active') return;
  218. document.getElementsByClassName('viewer-image active')[0].classList.remove('active');
  219. document.getElementsByClassName('page active')[0].classList.remove('active');
  220. this.classList.add('active');
  221. document.getElementById('page' + this.id).classList.add('active');
  222. if (document.getElementById('operation-menu').style.display == 'block') {
  223. document.getElementById('page' + nowIndex).removeChild($('.move')[0]);
  224. document.getElementById('select-menu').style.display = 'block';
  225. document.getElementById('operation-menu').style.display = 'none';
  226. }
  227. nowIndex = this.id;
  228. }
  229. if (imgIndex == 0) div.className = 'viewer-image active'
  230. div.innerHTML = '<img src="' + base64 + '">'
  231. document.getElementById('viewer-image').appendChild(div);
  232. if (positionList.length > 0) {
  233. positionList.filter(function(item) {
  234. return item.nowIndex == imgIndex
  235. }).forEach(function(item) {
  236. var div = document.createElement('div');
  237. div.className = item.moveType;
  238. div.innerHTML = '<img src="' + item.url + '">'
  239. div.style.left = item.left + '%';
  240. div.style.top = item.top + '%';
  241. document.getElementById('page' + item.nowIndex).appendChild(div);
  242. })
  243. }
  244. imgIndex++
  245. });
  246. }
  247. function createDom(i) {
  248. var div = document.createElement('div');
  249. div.className = 'page'
  250. div.id = 'page' + i;
  251. div.innerHTML = '<canvas id="the-canvas' + i + '"></canvas>';
  252. if (i == 0) div.className = 'page active'
  253. document.getElementById('viewer').appendChild(div);
  254. }
  255. PDFJS.getDocument({
  256. url: query.file
  257. }).promise.then(function(pdf) {
  258. for (var i = 0; i < pdf.pdfInfo.numPages; i++) {
  259. createDom(i);
  260. pdf.getPage(i + 1).then(renderPage)
  261. }
  262. })
  263. function btn(_self) {
  264. _self.parentNode.style.display = 'none'
  265. document.getElementById(_self.getAttribute('name')).style.display = 'block';
  266. switch (_self.id) {
  267. case 'seal':
  268. case 'sign':
  269. window.parent['signSeal'](_self.id, 'page' + nowIndex);
  270. break;
  271. case 'cancel':
  272. if (!document.getElementsByClassName('move')[0]) return;
  273. document.getElementById('page' + nowIndex).removeChild(document.getElementsByClassName('move')[0]);
  274. break;
  275. case 'sure':
  276. if (!document.getElementsByClassName('move')[0]) return;
  277. let obj = {
  278. left: parseFloat(parseInt(document.getElementsByClassName('move')[0].style.left) / document
  279. .getElementById('viewer').clientWidth * 100),
  280. top: top = parseFloat(parseInt(document.getElementsByClassName('move')[0].style.top) / document
  281. .getElementById('viewer').clientHeight * 100),
  282. moveType: moveType,
  283. nowIndex: nowIndex,
  284. url: url
  285. }
  286. if (moveType == 'seal') {
  287. obj['sealId'] = document.getElementsByClassName('move')[0].getAttribute('sealId');
  288. obj['recipient'] = document.getElementsByClassName('move')[0].getAttribute('recipient');
  289. obj['sealIsPass'] = false;
  290. }
  291. positionList.push(obj);
  292. noMove();
  293. document.getElementsByClassName('move')[0].classList.remove('move');
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. function createImage(type, imageUrl, elemId, recipient, sealId) {
  300. var div = document.createElement('div');
  301. div.className = type + ' move'
  302. div.setAttribute('recipient', recipient);
  303. div.setAttribute('sealId', sealId);
  304. div.innerHTML = '<img src="' + imageUrl + '">'
  305. document.getElementById(elemId).appendChild(div);
  306. moveType = type;
  307. url = imageUrl;
  308. move();
  309. }
  310. function move() {
  311. var oBox = document.getElementsByClassName("move")[0];
  312. oBox.onmousedown = function(ev) {
  313. var disX = ev.clientX - oBox.offsetLeft;
  314. var disY = ev.clientY - oBox.offsetTop;
  315. document.onmousemove = function(ev) {
  316. var l = ev.clientX - disX;
  317. var t = ev.clientY - disY;
  318. oBox.style.left = l + "px";
  319. oBox.style.top = t + "px";
  320. };
  321. document.onmouseup = function() {
  322. document.onmousemove = null;
  323. };
  324. return false; //阻止默认事件的发生
  325. };
  326. }
  327. function noMove() {
  328. var oBox = document.getElementsByClassName("move")[0];
  329. oBox.onmousedown = function(ev) {
  330. return false
  331. };
  332. }
  333. function sure() {
  334. return {
  335. url: query.fileUrl,
  336. fileId: query.fileId,
  337. fileName: query.fileName,
  338. positionList: positionList
  339. }
  340. }
  341. </script>
  342. </body>
  343. </html>