Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


JxBrowser Printing API allows you to decide whether web page should be printed using printer device or saved as PDF document.(通过JxBrowser Printing API,您可以决定是使用打印机设备打印网页还是将其保存为PDF文档。)


To save web page as PDF document you must register your own implementation of PrintHandler where you override default print settings and tell Chromium engine to save web page as PDF document at the specified path on the local file system. For example:(要将网页另存为PDF文档,您必须注册自己的PrintHandler实施,在其中您将覆盖默认打印设置,并告诉Chromium引擎在本地文件系统上的指定路径下将网页另存为PDF文档。例如:)

browser.setPrintHandler(new PrintHandler() {
    @Override
    public PrintStatus onPrint(PrintJob printJob) {
        PrintSettings settings = printJob.getPrintSettings();
        settings.setPrintToPDF(true);
        settings.setPDFFilePath("web_page.pdf");
        return PrintStatus.CONTINUE;
    }
});


Once you register PrintHandler that saves web page as PDF document, you can initiate printing using one of the following ways:(注册将网页另存为PDF文档的PrintHandler之后,您可以使用以下方式之一启动打印:)

  • By invoking the Browser.print() method.(通过调用Browser.print()方法。)
  • By invoking the window.print() JavaScript function.(通过调用window.print()JavaScript函数。)

Now, every time when printing is requested from Java or JavaScript code, the PrintHandler will be used where you configure print settings to save web page as PDF document at specified file on the local file system. (现在,每次从Java或JavaScript代码请求打印时,都会使用PrintHandler来配置打印设置,以将网页另存为PDF文档,并将其保存在本地文件系统上的指定文件中)
import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.PrintJob;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * The sample demonstrates how to print currently loaded web page in
 * a PDF file with custom print settings.
 */
public class PrintToPDFSample {
    public static void main(String[] args) {
        final Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        browser.setPrintHandler(new PrintHandler() {
            @Override
            public PrintStatus onPrint(PrintJob printJob) {
                PrintSettings settings = printJob.getPrintSettings();
                settings.setPrintToPDF(true);
                settings.setPDFFilePath("web_page.pdf");
                settings.setPrintBackgrounds(true);
                return PrintStatus.CONTINUE;
            }
        });

        final JButton print = new JButton("Print to PDF");
        print.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                browser.print();
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(print, BorderLayout.NORTH);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.loadURL("http://google.com");
    }
}

Note: in your PrintHandler implementation you can display UI dialog where user can decide whether web page should be printed using printer device or saved as PDF document.(注意:在您的PrintHandler实施中,您可以显示UI对话框,用户可以在其中决定是使用打印机设备打印网页还是将其保存为PDF文档。)