download_manager.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DownloadManager = void 0;
  27. var _pdf = require("../pdf");
  28. ;
  29. const DISABLE_CREATE_OBJECT_URL = _pdf.apiCompatibilityParams.disableCreateObjectURL || false;
  30. function download(blobUrl, filename) {
  31. const a = document.createElement("a");
  32. if (!a.click) {
  33. throw new Error('DownloadManager: "a.click()" is not supported.');
  34. }
  35. a.href = blobUrl;
  36. a.target = "_parent";
  37. if ("download" in a) {
  38. a.download = filename;
  39. }
  40. (document.body || document.documentElement).appendChild(a);
  41. a.click();
  42. a.remove();
  43. }
  44. class DownloadManager {
  45. constructor({
  46. disableCreateObjectURL = DISABLE_CREATE_OBJECT_URL
  47. }) {
  48. this.disableCreateObjectURL = disableCreateObjectURL;
  49. }
  50. downloadUrl(url, filename) {
  51. if (!(0, _pdf.createValidAbsoluteUrl)(url, "http://example.com")) {
  52. return;
  53. }
  54. download(url + "#pdfjs.action=download", filename);
  55. }
  56. downloadData(data, filename, contentType) {
  57. if (navigator.msSaveBlob) {
  58. navigator.msSaveBlob(new Blob([data], {
  59. type: contentType
  60. }), filename);
  61. return;
  62. }
  63. const blobUrl = (0, _pdf.createObjectURL)(data, contentType, this.disableCreateObjectURL);
  64. download(blobUrl, filename);
  65. }
  66. download(blob, url, filename) {
  67. if (navigator.msSaveBlob) {
  68. if (!navigator.msSaveBlob(blob, filename)) {
  69. this.downloadUrl(url, filename);
  70. }
  71. return;
  72. }
  73. if (this.disableCreateObjectURL) {
  74. this.downloadUrl(url, filename);
  75. return;
  76. }
  77. const blobUrl = URL.createObjectURL(blob);
  78. download(blobUrl, filename);
  79. }
  80. }
  81. exports.DownloadManager = DownloadManager;