Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: components/printing/renderer/print_web_view_helper_mac.mm

Issue 2780433002: add print to pdf for headless (Closed)
Patch Set: fix mac page setting Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/printing/renderer/print_web_view_helper.h" 5 #include "components/printing/renderer/print_web_view_helper.h"
6 6
7 #import <AppKit/AppKit.h> 7 #import <AppKit/AppKit.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/mac/scoped_nsautorelease_pool.h" 10 #include "base/mac/scoped_nsautorelease_pool.h"
(...skipping 12 matching lines...) Expand all
23 int page_count) { 23 int page_count) {
24 const PrintMsg_PrintPages_Params& params = *print_pages_params_; 24 const PrintMsg_PrintPages_Params& params = *print_pages_params_;
25 const PrintMsg_Print_Params& print_params = params.params; 25 const PrintMsg_Print_Params& print_params = params.params;
26 26
27 std::vector<int> printed_pages = GetPrintedPages(params, page_count); 27 std::vector<int> printed_pages = GetPrintedPages(params, page_count);
28 if (printed_pages.empty()) 28 if (printed_pages.empty())
29 return false; 29 return false;
30 30
31 PrintMsg_PrintPage_Params page_params; 31 PrintMsg_PrintPage_Params page_params;
32 page_params.params = print_params; 32 page_params.params = print_params;
33 for (int page_number : printed_pages) { 33
34 page_params.page_number = page_number; 34 if (delegate_->UseSingleMetafile()) {
35 PrintPageInternal(page_params, frame); 35 PrintPagesInternal(page_params, frame, printed_pages);
36 return true;
36 } 37 }
38
39 for (int page_number : printed_pages)
40 PrintPagesInternal(page_params, frame, std::vector<int>{page_number});
37 return true; 41 return true;
38 } 42 }
39 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) 43 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
40 44
41 void PrintWebViewHelper::PrintPageInternal( 45 void PrintWebViewHelper::PrintPagesInternal(
42 const PrintMsg_PrintPage_Params& params, 46 const PrintMsg_PrintPage_Params& params,
43 blink::WebLocalFrame* frame) { 47 blink::WebLocalFrame* frame,
48 std::vector<int> printed_pages) {
44 PdfMetafileSkia metafile(PDF_SKIA_DOCUMENT_TYPE); 49 PdfMetafileSkia metafile(PDF_SKIA_DOCUMENT_TYPE);
45 CHECK(metafile.Init()); 50 CHECK(metafile.Init());
46 51
47 int page_number = params.page_number;
48 gfx::Size page_size_in_dpi; 52 gfx::Size page_size_in_dpi;
49 gfx::Rect content_area_in_dpi; 53 gfx::Rect content_area_in_dpi;
Lei Zhang 2017/04/06 08:08:42 In principle, due to fancy CSS, this can be differ
jzfeng 2017/04/06 09:01:51 I feel it is ok to leave as is? For the normal ca
Lei Zhang 2017/04/06 10:23:00 That assumes nobody else will try to use UseSingle
jzfeng 2017/04/07 03:24:06 Done. Added this potential bug to the comment of t
50 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile, 54 for (int page_number : printed_pages) {
51 &page_size_in_dpi, &content_area_in_dpi); 55 RenderPage(params.params, page_number, frame, false, &metafile,
56 &page_size_in_dpi, &content_area_in_dpi);
57 }
52 metafile.FinishDocument(); 58 metafile.FinishDocument();
53 59
54 PrintHostMsg_DidPrintPage_Params page_params; 60 PrintHostMsg_DidPrintPage_Params page_params;
55 page_params.data_size = metafile.GetDataSize(); 61 page_params.data_size = metafile.GetDataSize();
56 page_params.page_number = page_number;
57 page_params.document_cookie = params.params.document_cookie; 62 page_params.document_cookie = params.params.document_cookie;
58 page_params.page_size = page_size_in_dpi; 63 page_params.page_size = page_size_in_dpi;
59 page_params.content_area = content_area_in_dpi; 64 page_params.content_area = content_area_in_dpi;
60 65
61 // Ask the browser to create the shared memory for us. 66 // Ask the browser to create the shared memory for us.
62 if (!CopyMetafileDataToSharedMem(metafile, 67 if (!CopyMetafileDataToSharedMem(metafile,
63 &(page_params.metafile_data_handle))) { 68 &(page_params.metafile_data_handle))) {
64 // TODO(thestig): Fail and return false instead. 69 // TODO(thestig): Fail and return false instead.
65 page_params.data_size = 0; 70 page_params.data_size = 0;
66 } 71 }
67 72
68 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); 73 for (int page_number : printed_pages) {
74 page_params.page_number = page_number;
75 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
76 page_params.metafile_data_handle = base::SharedMemoryHandle();
77 }
69 } 78 }
70 79
71 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) 80 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
72 bool PrintWebViewHelper::RenderPreviewPage( 81 bool PrintWebViewHelper::RenderPreviewPage(
73 int page_number, 82 int page_number,
74 const PrintMsg_Print_Params& print_params) { 83 const PrintMsg_Print_Params& print_params) {
75 PrintMsg_Print_Params printParams = print_params; 84 PrintMsg_Print_Params printParams = print_params;
76 std::unique_ptr<PdfMetafileSkia> draft_metafile; 85 std::unique_ptr<PdfMetafileSkia> draft_metafile;
77 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); 86 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
78 87
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) 160 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
152 RenderPageContent(frame, page_number, canvas_area, content_area, 161 RenderPageContent(frame, page_number, canvas_area, content_area,
153 scale_factor, static_cast<blink::WebCanvas*>(canvas)); 162 scale_factor, static_cast<blink::WebCanvas*>(canvas));
154 } 163 }
155 164
156 // Done printing. Close the device context to retrieve the compiled metafile. 165 // Done printing. Close the device context to retrieve the compiled metafile.
157 metafile->FinishPage(); 166 metafile->FinishPage();
158 } 167 }
159 168
160 } // namespace printing 169 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698