1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PDF_PDF_H_
#define PDF_PDF_H_
#include <vector>
#include "base/containers/span.h"
#include "base/optional.h"
#include "base/values.h"
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
#if defined(OS_WIN)
typedef void (*PDFEnsureTypefaceCharactersAccessible)(const LOGFONT* font,
const wchar_t* text,
size_t text_length);
#endif
namespace gfx {
class Rect;
class Size;
class SizeF;
} // namespace gfx
namespace chrome_pdf {
#if defined(OS_CHROMEOS)
// Create a flattened PDF document from an existing PDF document.
// |input_buffer| is the buffer that contains the entire PDF document to be
// flattened.
std::vector<uint8_t> CreateFlattenedPdf(base::span<const uint8_t> input_buffer);
#endif // defined(OS_CHROMEOS)
#if defined(OS_WIN)
// Printing modes - type to convert PDF to for printing. See PDFium's
// FPDF_SetPrintMode() for details.
enum PrintingMode {
kEmf = 0,
kTextOnly = 1,
kPostScript2 = 2,
kPostScript3 = 3,
// Values 4 and 5 are similar to |kPostScript2| and |kPostScript3|, but are
// not intended for use in sandboxed environments like Chromium's.
kEmfWithReducedRasterization = 6,
};
// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
// |page_number| is the 0-based index of the page to be rendered.
// |dc| is the device context to render into.
// |dpi_x| and |dpi_y| is the resolution.
// |bounds_origin_x|, |bounds_origin_y|, |bounds_width| and |bounds_height|
// specify a bounds rectangle within the DC in which to render the PDF
// page.
// |fit_to_bounds| specifies whether the output should be shrunk to fit the
// supplied bounds if the page size is larger than the bounds in any
// dimension. If this is false, parts of the PDF page that lie outside
// the bounds will be clipped.
// |stretch_to_bounds| specifies whether the output should be stretched to fit
// the supplied bounds if the page size is smaller than the bounds in any
// dimension.
// If both |fit_to_bounds| and |stretch_to_bounds| are true, then
// |fit_to_bounds| is honored first.
// |keep_aspect_ratio| If any scaling is to be done is true, this flag
// specifies whether the original aspect ratio of the page should be
// preserved while scaling.
// |center_in_bounds| specifies whether the final image (after any scaling is
// done) should be centered within the given bounds.
// |autorotate| specifies whether the final image should be rotated to match
// the output bound.
// |use_color| specifies color or grayscale.
// Returns false if the document or the page number are not valid.
bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
int page_number,
HDC dc,
int dpi_x,
int dpi_y,
int bounds_origin_x,
int bounds_origin_y,
int bounds_width,
int bounds_height,
bool fit_to_bounds,
bool stretch_to_bounds,
bool keep_aspect_ratio,
bool center_in_bounds,
bool autorotate,
bool use_color);
void SetPDFEnsureTypefaceCharactersAccessible(
PDFEnsureTypefaceCharactersAccessible func);
void SetPDFUseGDIPrinting(bool enable);
void SetPDFUsePrintMode(int mode);
#endif // defined(OS_WIN)
// |page_count| and |max_page_width| are optional and can be NULL.
// Returns false if the document is not valid.
bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int* page_count,
float* max_page_width);
// Whether the PDF is Tagged (see 10.7 "Tagged PDF" in PDF Reference 1.7).
// Returns true if it's a tagged (accessible) PDF, false if it's a valid
// PDF but untagged, and nullopt if the PDF can't be parsed.
base::Optional<bool> IsPDFDocTagged(base::span<const uint8_t> pdf_buffer);
// Given a tagged PDF (see IsPDFDocTagged, above), return the portion of
// the structure tree for a given page as a hierarchical tree of base::Values.
base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
int page_index);
// Gets the dimensions of a specific page in a document.
// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
// |page_number| is the page number that the function will get the dimensions
// of.
// Returns the size of the page in points, or nullopt if the document or the
// page number are not valid.
base::Optional<gfx::SizeF> GetPDFPageSizeByIndex(
base::span<const uint8_t> pdf_buffer,
int page_number);
enum class RenderDeviceType {
kDisplay,
kPrinter,
};
struct RenderOptions {
// Whether the output should be stretched to fit the supplied bitmap.
bool stretch_to_bounds;
// If any scaling is needed, whether the original aspect ratio of the page is
// preserved while scaling.
bool keep_aspect_ratio;
// Whether the final image should be rotated to match the output bound.
bool autorotate;
// Specifies color or grayscale.
bool use_color;
// What type of device to render for.
RenderDeviceType render_device_type;
};
// Renders PDF page into 4-byte per pixel BGRA color bitmap.
// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
// |page_number| is the 0-based index of the page to be rendered.
// |bitmap_buffer| is the output buffer for bitmap.
// |bitmap_size| is the size of the output bitmap.
// |dpi| is the 2D resolution.
// |options| is the options to render with.
// Returns false if the document or the page number are not valid.
bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int page_number,
void* bitmap_buffer,
const gfx::Size& bitmap_size,
const gfx::Size& dpi,
const RenderOptions& options);
// Convert multiple PDF pages into a N-up PDF.
// |input_buffers| is the vector of buffers with each buffer contains a PDF.
// If any of the PDFs contains multiple pages, only the first page of the
// document is used.
// |pages_per_sheet| is the number of pages to put on one sheet.
// |page_size| is the output page size, measured in PDF "user space" units.
// |printable_area| is the output page printable area, measured in PDF
// "user space" units. Should be smaller than |page_size|.
//
// |page_size| is the print media size. The page size of the output N-up PDF is
// determined by the |pages_per_sheet|, the orientation of the PDF pages
// contained in the |input_buffers|, and the media page size |page_size|. For
// example, when |page_size| = 512x792, |pages_per_sheet| = 2, and the
// orientation of |input_buffers| = portrait, the output N-up PDF will be
// 792x512.
// See printing::NupParameters for more details on how the output page
// orientation is determined, to understand why |page_size| may be swapped in
// some cases.
std::vector<uint8_t> ConvertPdfPagesToNupPdf(
std::vector<base::span<const uint8_t>> input_buffers,
size_t pages_per_sheet,
const gfx::Size& page_size,
const gfx::Rect& printable_area);
// Convert a PDF document to a N-up PDF document.
// |input_buffer| is the buffer that contains the entire PDF document to be
// converted to a N-up PDF document.
// |pages_per_sheet| is the number of pages to put on one sheet.
// |page_size| is the output page size, measured in PDF "user space" units.
// |printable_area| is the output page printable area, measured in PDF
// "user space" units. Should be smaller than |page_size|.
//
// Refer to the description of ConvertPdfPagesToNupPdf to understand how the
// output page size will be calculated.
// The algorithm used to determine the output page size is the same.
std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
base::span<const uint8_t> input_buffer,
size_t pages_per_sheet,
const gfx::Size& page_size,
const gfx::Rect& printable_area);
} // namespace chrome_pdf
#endif // PDF_PDF_H_
|