使用JavaScript在Thunderbird中以编程方式将电子邮件(标题和正文)保存为PDF

时间:2017-08-22 23:30:06

标签: thunderbird thunderbird-addon

我正在尝试以编程方式在Thunderbird中将电子邮件另存为PDF。

我已经能够成功保存电子邮件的正文,但我无法包含标题(来自,来自,日期,主题等)。

我基本上想要获得与File>类似的输出打印。

以下是我正在使用的内容。



function printEmail() {
  var outputFormat = Components.interfaces.nsIPrintSettings.kOutputFormatPDF;

  var webBrowserPrint = window.content.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    .getInterface(Components.interfaces.nsIWebBrowserPrint);

  var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
    .getService(Components.interfaces.nsIPrintSettingsService);

  var printSettings = PSSVC.newPrintSettings;
  var printerName = "";
  try {
    printerName = "printer_" + PSSVC.defaultPrinterName.replace(" ", "_", "g");
  } catch (e) {
    alert(e);
  }

  printSettings.printToFile = true;
  printSettings.toFileName = completeFilePath;
  printSettings.printSilent = true;
  printSettings.outputFormat = outputFormat;

  webBrowserPrint.print(printSettings, null);
}




1 个答案:

答案 0 :(得分:0)

您使用的webBrowserPrint.print函数似乎是一个通用函数,用于打印各种内容。我认为它是由更高级别的功能调用的。我试图追踪这个,这就是我找到的。

实现的UI打印命令 https://dxr.mozilla.org/thunderbird/source/mail/base/content/messageWindow.js

调用PrintEnginePrint(),会打开msgPrintEngine.xul并触发OnLoadPrintEngine()

更深入的内部呼叫DisplayMessageForPrinting,设置标志mPrintingOperation。因此,正在打印的网址会附加额外的参数&header=print

我试图复制OnLoadPrintEngine()所做的事情,在途中修改打印设置。但是,我无法让它发挥作用:

var webBrowserPrint = window.content.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIWebBrowserPrint);
var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintSettingsService);
var printEngine = Components.classes["@mozilla.org/messenger/msgPrintEngine;1"].createInstance();
printEngine = printEngine.QueryInterface(Components.interfaces.nsIMsgPrintEngine);
printEngine.setWindow(window);
var printSettings = PSSVC.globalPrintSettings; //Any changes to the settings go here.
printEngine.startPrintOperation(printSettings);

但是,这只会导致我的TB窗口消失。