modules/autoprint.js

  1. /** @license
  2. * jsPDF Autoprint Plugin
  3. *
  4. * Licensed under the MIT License.
  5. * http://opensource.org/licenses/mit-license
  6. */
  7. import { jsPDF } from "../jspdf.js";
  8. /**
  9. * @name autoprint
  10. * @module
  11. */
  12. (function(jsPDFAPI) {
  13. "use strict";
  14. /**
  15. * Makes the PDF automatically open the print-Dialog when opened in a PDF-viewer.
  16. *
  17. * @name autoPrint
  18. * @function
  19. * @param {Object} options (optional) Set the attribute variant to 'non-conform' (default) or 'javascript' to activate different methods of automatic printing when opening in a PDF-viewer .
  20. * @returns {jsPDF}
  21. * @example
  22. * var doc = new jsPDF();
  23. * doc.text(10, 10, 'This is a test');
  24. * doc.autoPrint({variant: 'non-conform'});
  25. * doc.save('autoprint.pdf');
  26. */
  27. jsPDFAPI.autoPrint = function(options) {
  28. "use strict";
  29. var refAutoPrintTag;
  30. options = options || {};
  31. options.variant = options.variant || "non-conform";
  32. switch (options.variant) {
  33. case "javascript":
  34. //https://github.com/Rob--W/pdf.js/commit/c676ecb5a0f54677b9f3340c3ef2cf42225453bb
  35. this.addJS("print({});");
  36. break;
  37. case "non-conform":
  38. default:
  39. this.internal.events.subscribe("postPutResources", function() {
  40. refAutoPrintTag = this.internal.newObject();
  41. this.internal.out("<<");
  42. this.internal.out("/S /Named");
  43. this.internal.out("/Type /Action");
  44. this.internal.out("/N /Print");
  45. this.internal.out(">>");
  46. this.internal.out("endobj");
  47. });
  48. this.internal.events.subscribe("putCatalog", function() {
  49. this.internal.out("/OpenAction " + refAutoPrintTag + " 0 R");
  50. });
  51. break;
  52. }
  53. return this;
  54. };
  55. })(jsPDF.API);