Skip to content

Commit

Permalink
Adding "Print to PDF" functionality in the print preview tab.
Browse files Browse the repository at this point in the history
BUG=69508
TEST=Linux, Mac only: Run chrome with flag --enable-print-preview, hit ctrl+p, select Print to PDF from the printer list and hit the print button, select a filename and save. A pdf file should be generated at the specified location.

Review URL: http://codereview.chromium.org/6698074

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@79941 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dpapad@chromium.org committed Mar 31, 2011
1 parent 089a5d6 commit bb27c9b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
4 changes: 3 additions & 1 deletion chrome/browser/resources/print_preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function getSettingsJSON() {
var collate = $('collate').checked;
var landscape = isLandscape();
var color = isColor();
var printToPDF = (printerName == localStrings.getString('printToPDF'));

return JSON.stringify({'printerName': printerName,
'pageRange': pageRangesInfo,
Expand All @@ -203,7 +204,8 @@ function getSettingsJSON() {
'copies': copies,
'collate': collate,
'landscape': landscape,
'color': color});
'color': color,
'printToPDF': printToPDF});
}

/**
Expand Down
80 changes: 79 additions & 1 deletion chrome/browser/ui/webui/print_preview_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
#include "base/json/json_reader.h"
#include "base/threading/thread.h"
#include "base/values.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/printing/print_preview_tab_controller.h"
#include "chrome/browser/ui/webui/print_preview_ui_html_source.h"
#include "chrome/browser/ui/webui/print_preview_ui.h"
#include "chrome/common/print_messages.h"
#include "content/browser/browser_thread.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "printing/backend/print_backend.h"
#include "printing/native_metafile_factory.h"
#include "printing/native_metafile.h"
#include "printing/print_job_constants.h"

namespace {
Expand Down Expand Up @@ -105,6 +110,29 @@ class EnumeratePrintersTaskProxy
DISALLOW_COPY_AND_ASSIGN(EnumeratePrintersTaskProxy);
};

// A Task implementation that stores a PDF file on disk.
class PrintToPdfTask : public Task {
public:
// Takes ownership of |metafile|.
PrintToPdfTask(printing::NativeMetafile* metafile, const FilePath& path)
: metafile_(metafile), path_(path) {
}

~PrintToPdfTask() {}

// Task implementation
virtual void Run() {
metafile_->SaveTo(path_);
}

private:
// The metafile holding the PDF data.
scoped_ptr<printing::NativeMetafile> metafile_;

// The absolute path where the file will be saved.
FilePath path_;
};

PrintPreviewHandler::PrintPreviewHandler()
: print_backend_(printing::PrintBackend::CreateInstance(NULL)),
need_to_generate_preview_(true),
Expand All @@ -113,6 +141,8 @@ PrintPreviewHandler::PrintPreviewHandler()
}

PrintPreviewHandler::~PrintPreviewHandler() {
if (select_file_dialog_.get())
select_file_dialog_->ListenerDestroyed();
}

void PrintPreviewHandler::RegisterMessages() {
Expand Down Expand Up @@ -163,7 +193,14 @@ void PrintPreviewHandler::HandlePrint(const ListValue* args) {
scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args));
if (!settings.get())
return;
web_ui_->GetRenderViewHost()->PrintForPrintPreview(*settings);

bool print_to_pdf;
settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf);

if (print_to_pdf)
SelectFile();
else
web_ui_->GetRenderViewHost()->PrintForPrintPreview(*settings);
}

void PrintPreviewHandler::SendPrinterList(const ListValue& printers) {
Expand All @@ -189,3 +226,44 @@ void PrintPreviewHandler::ProcessLandscapeSetting(
need_to_generate_preview_ = true;
}
}

void PrintPreviewHandler::SelectFile() {
SelectFileDialog::FileTypeInfo file_type_info;
file_type_info.extensions.resize(1);
file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf"));

if (!select_file_dialog_.get())
select_file_dialog_ = SelectFileDialog::Create(this);

select_file_dialog_->SelectFile(
SelectFileDialog::SELECT_SAVEAS_FILE,
string16(),
FilePath(),
&file_type_info,
0,
FILE_PATH_LITERAL(""),
platform_util::GetTopLevel(
web_ui_->tab_contents()->GetNativeView()),
NULL);
}

void PrintPreviewHandler::FileSelected(const FilePath& path,
int index, void* params) {
#if defined(OS_POSIX)
PrintPreviewUIHTMLSource::PrintPreviewData data;
PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_);
pp_ui->html_source()->GetPrintPreviewData(&data);
DCHECK(data.first != NULL);
DCHECK(data.second > 0);

printing::NativeMetafile* metafile =
printing::NativeMetafileFactory::CreateFromData(data.first->memory(),
data.second);
metafile->FinishDocument();

PrintToPdfTask* task = new PrintToPdfTask(metafile, path);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task);
#else
NOTIMPLEMENTED();
#endif
}
13 changes: 12 additions & 1 deletion chrome/browser/ui/webui/print_preview_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/shell_dialogs.h"
#include "content/browser/webui/web_ui.h"

class EnumeratePrintersTaskProxy;
Expand All @@ -18,14 +19,21 @@ class PrintBackend;

// The handler for Javascript messages related to the "print preview" dialog.
class PrintPreviewHandler : public WebUIMessageHandler,
public base::SupportsWeakPtr<PrintPreviewHandler> {
public base::SupportsWeakPtr<PrintPreviewHandler>,
public SelectFileDialog::Listener {
public:
PrintPreviewHandler();
virtual ~PrintPreviewHandler();

// WebUIMessageHandler implementation.
virtual void RegisterMessages();

// SelectFileDialog::Listener implementation.
virtual void FileSelected(const FilePath& path, int index, void* params);

// Displays a modal dialog, prompting the user to select a file.
void SelectFile();

private:
friend class EnumeratePrintersTaskProxy;

Expand Down Expand Up @@ -61,6 +69,9 @@ class PrintPreviewHandler : public WebUIMessageHandler,
// Set to true if the preview should be landscape.
bool landscape_;

// The underlying dialog object.
scoped_refptr<SelectFileDialog> select_file_dialog_;

DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler);
};

Expand Down
3 changes: 3 additions & 0 deletions printing/print_job_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const char kSettingColor[] = "color";
// Page orientation: true for landscape, false for portrait.
const char kSettingLandscape[] = "landscape";

// Print to PDF option: true if selected, false if not.
const char kSettingPrintToPDF[] = "printToPDF";

// Printer name.
const char kSettingPrinterName[] = "printerName";

Expand Down
1 change: 1 addition & 0 deletions printing/print_job_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace printing {

extern const char kSettingColor[];
extern const char kSettingLandscape[];
extern const char kSettingPrintToPDF[];
extern const char kSettingPrinterName[];

} // namespace printing
Expand Down

0 comments on commit bb27c9b

Please sign in to comment.