Skip to content

PDF printing support #362

Open
Open
@cztomczak

Description

@cztomczak

Printing to PDF can be performed in two ways:

  1. When print dialog appears you should see "Print to PDF" option (printer). This is already available.

  2. There is a new method CefBrowserHost::PrintToPDF(), but this wasn't yet exposed in CEF Python.

See these commits by cuijinbao for reference: aec3474 and 7dc6d7d .

Below is the code from CEF header files that would need to be exposed:

PrintToPDF function

///
  // Print the current browser contents to the PDF file specified by |path| and
  // execute |callback| on completion. The caller is responsible for deleting
  // |path| when done. For PDF printing to work on Linux you must implement the
  // CefPrintHandler::GetPdfPaperSize method.
  ///
  /*--cef(optional_param=callback)--*/
  virtual void PrintToPDF(const CefString& path,
                          const CefPdfPrintSettings& settings,
                          CefRefPtr<CefPdfPrintCallback> callback) =0;

Print settings

///
// Structure representing PDF print settings.
///
typedef struct _cef_pdf_print_settings_t {
  ///
  // Page title to display in the header. Only used if |header_footer_enabled|
  // is set to true (1).
  ///
  cef_string_t header_footer_title;

  ///
  // URL to display in the footer. Only used if |header_footer_enabled| is set
  // to true (1).
  ///
  cef_string_t header_footer_url;

  ///
  // Output page size in microns. If either of these values is less than or
  // equal to zero then the default paper size (A4) will be used.
  ///
  int page_width;
  int page_height;

  ///
  // The percentage to scale the PDF by before printing (e.g. 50 is 50%).
  // If this value is less than or equal to zero the default value of 100
  // will be used.
  ///
  int scale_factor;

  ///
  // Margins in millimeters. Only used if |margin_type| is set to
  // PDF_PRINT_MARGIN_CUSTOM.
  ///
  double margin_top;
  double margin_right;
  double margin_bottom;
  double margin_left;

  ///
  // Margin type.
  ///
  cef_pdf_print_margin_type_t margin_type;

  ///
  // Set to true (1) to print headers and footers or false (0) to not print
  // headers and footers.
  ///
  int header_footer_enabled;

  ///
  // Set to true (1) to print the selection only or false (0) to print all.
  ///
  int selection_only;

  ///
  // Set to true (1) for landscape mode or false (0) for portrait mode.
  ///
  int landscape;

  ///
  // Set to true (1) to print background graphics or false (0) to not print
  // background graphics.
  ///
  int backgrounds_enabled;

} cef_pdf_print_settings_t;

Print callback

///
// Callback interface for CefBrowserHost::PrintToPDF. The methods of this class
// will be called on the browser process UI thread.
///
/*--cef(source=client)--*/
class CefPdfPrintCallback : public virtual CefBaseRefCounted {
 public:
  ///
  // Method that will be executed when the PDF printing has completed. |path|
  // is the output path. |ok| will be true if the printing completed
  // successfully or false otherwise.
  ///
  /*--cef()--*/
  virtual void OnPdfPrintFinished(const CefString& path, bool ok) =0;
};

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions