JxBrowser 将 html 转换为带有 7.X 版本的 pdf

JxBrowser comvert html to pdf with 7.X release

我想使用 TeamDev jxbrowser 将 html 文件转换为 pdf。我想通过执行一些代码自动触发它而无需任何额外的打印对话框或弹出窗口等。而且我还想设置一些额外的设置。在 6.X 版本中,我可以通过编码

查看更详细的选项

https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013120-print-settings

browser.setPrintHandler(new PrintHandler() {
            @Override
            public PrintStatus onPrint(PrintJob printJob) {
                PrintSettings printSettings = printJob.getPrintSettings();
                printSettings.setLandscape(true);

但在 7.x 版本中,很少有关于通过代码逐步自定义 pdf 生成的详细信息。

https://jxbrowser-support.teamdev.com/docs/guides/printing.html#print-preview

允许使用 PDF 打印机以编程方式打印当前加载的网页的打印 API 在 JxBrowser 7 中不可用。它已经在我们的 roadmap 上。我们会尽力在下一个版本中引入此功能。

我建议您在 https://twitter.com/JxBrowserTeam 上关注我们,以便在发布此功能时收到通知。

UPD:在 JxBrowser 7.13 中,打印 API 已经扩展了允许以编程方式配置打印设置和打印网页而不显示打印预览对话框的功能。 API 允许您将当前加载的网页保存为 PDF 文档。在 https://jxbrowser-support.teamdev.com/docs/guides/printing.html#configuring-settings

阅读更多内容

以下示例演示如何将网页另存为 PDF:

import static com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN;
import static com.teamdev.jxbrowser.print.Orientation.PORTRAIT;

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.browser.callback.PrintCallback;
import com.teamdev.jxbrowser.browser.callback.PrintHtmlCallback;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.frame.Frame;
import com.teamdev.jxbrowser.print.PdfPrinter;
import com.teamdev.jxbrowser.print.PdfPrinter.HtmlSettings;
import com.teamdev.jxbrowser.print.PrintJob;
import com.teamdev.jxbrowser.print.event.PrintCompleted;
import java.nio.file.Paths;

/**
 * This example demonstrates how to configure print settings programmatically and print the
 * currently loaded web page using the built-in PDF printer. In general, it shows how to save the
 * currently loaded web page as a PDF document.
 */
public final class PrintToPdf {

    public static void main(String[] args) {
        Engine engine = Engine.newInstance(OFF_SCREEN);
        Browser browser = engine.newBrowser();
        browser.set(PrintCallback.class, (params, tell) -> tell.print());
        browser.set(PrintHtmlCallback.class, (params, tell) -> {
            PdfPrinter<PdfPrinter.HtmlSettings> pdfPrinter =
                    params.printers().pdfPrinter();
            PrintJob<HtmlSettings> printJob = pdfPrinter.printJob();
            printJob.settings()
                    .pdfFilePath(Paths.get("google.pdf").toAbsolutePath())
                    .enablePrintingBackgrounds()
                    .orientation(PORTRAIT)
                    .apply();
            printJob.on(PrintCompleted.class, event -> {
                if (event.isSuccess()) {
                    System.out.println("Printing is completed successfully.");
                } else {
                    System.out.println("Printing has failed.");
                }
            });
            tell.proceed(pdfPrinter);
        });
        browser.navigation().loadUrlAndWait("https://google.com");
        browser.mainFrame().ifPresent(Frame::print);
    }
}