1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "pdf/out_of_process_instance.h"
14 #include "pdf/pdf_ppapi.h"
16 namespace chrome_pdf {
20 class ScopedSdkInitializer {
22 ScopedSdkInitializer() {}
23 ~ScopedSdkInitializer() {
27 if (!IsSDKInitializedViaPepper())
36 return IsSDKInitializedViaPepper() || InitializeSDK();
41 bool initialized_ = false;
44 DISALLOW_COPY_AND_ASSIGN(ScopedSdkInitializer);
50 bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
60 bool stretch_to_bounds,
61 bool keep_aspect_ratio,
62 bool center_in_bounds,
65 ScopedSdkInitializer scoped_sdk_initializer;
66 if (!scoped_sdk_initializer.Init())
69 PDFEngineExports* engine_exports = PDFEngineExports::Get();
70 PDFEngineExports::RenderingSettings settings(
72 pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height),
73 fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
74 autorotate, use_color);
75 return engine_exports->RenderPDFPageToDC(pdf_buffer, page_number, settings,
79 void SetPDFEnsureTypefaceCharactersAccessible(
80 PDFEnsureTypefaceCharactersAccessible func) {
81 PDFEngineExports::Get()->SetPDFEnsureTypefaceCharactersAccessible(func);
84 void SetPDFUseGDIPrinting(bool enable) {
85 PDFEngineExports::Get()->SetPDFUseGDIPrinting(enable);
88 void SetPDFUsePrintMode(int mode) {
89 PDFEngineExports::Get()->SetPDFUsePrintMode(mode);
91 #endif // defined(OS_WIN)
93 bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
95 double* max_page_width) {
96 ScopedSdkInitializer scoped_sdk_initializer;
97 if (!scoped_sdk_initializer.Init())
100 PDFEngineExports* engine_exports = PDFEngineExports::Get();
101 return engine_exports->GetPDFDocInfo(pdf_buffer, page_count, max_page_width);
104 bool GetPDFPageSizeByIndex(base::span<const uint8_t> pdf_buffer,
108 ScopedSdkInitializer scoped_sdk_initializer;
109 if (!scoped_sdk_initializer.Init())
112 chrome_pdf::PDFEngineExports* engine_exports =
113 chrome_pdf::PDFEngineExports::Get();
114 return engine_exports->GetPDFPageSizeByIndex(pdf_buffer, page_number, width,
118 bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
127 ScopedSdkInitializer scoped_sdk_initializer;
128 if (!scoped_sdk_initializer.Init())
131 PDFEngineExports* engine_exports = PDFEngineExports::Get();
132 PDFEngineExports::RenderingSettings settings(
133 dpi_x, dpi_y, pp::Rect(bitmap_width, bitmap_height), true, false, true,
134 true, autorotate, use_color);
135 return engine_exports->RenderPDFPageToBitmap(pdf_buffer, page_number,
136 settings, bitmap_buffer);
139 std::vector<uint8_t> ConvertPdfPagesToNupPdf(
140 std::vector<base::span<const uint8_t>> input_buffers,
141 size_t pages_per_sheet,
142 const gfx::Size& page_size,
143 const gfx::Rect& printable_area) {
144 ScopedSdkInitializer scoped_sdk_initializer;
145 if (!scoped_sdk_initializer.Init())
146 return std::vector<uint8_t>();
148 PDFEngineExports* engine_exports = PDFEngineExports::Get();
149 return engine_exports->ConvertPdfPagesToNupPdf(
150 std::move(input_buffers), pages_per_sheet, page_size, printable_area);
153 std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
154 base::span<const uint8_t> input_buffer,
155 size_t pages_per_sheet,
156 const gfx::Size& page_size,
157 const gfx::Rect& printable_area) {
158 ScopedSdkInitializer scoped_sdk_initializer;
159 if (!scoped_sdk_initializer.Init())
160 return std::vector<uint8_t>();
162 PDFEngineExports* engine_exports = PDFEngineExports::Get();
163 return engine_exports->ConvertPdfDocumentToNupPdf(
164 input_buffer, pages_per_sheet, page_size, printable_area);
167 } // namespace chrome_pdf