1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "base/containers/span.h"
11 #include "base/values.h"
12 #include "build/build_config.h"
24 namespace chrome_pdf {
26 void SetUseSkiaRendererPolicy(bool use_skia);
28 #if BUILDFLAG(IS_CHROMEOS)
29 // Create a flattened PDF document from an existing PDF document.
30 // `input_buffer` is the buffer that contains the entire PDF document to be
32 std::vector<uint8_t> CreateFlattenedPdf(base::span<const uint8_t> input_buffer);
33 #endif // BUILDFLAG(IS_CHROMEOS)
36 // Printing modes - type to convert PDF to for printing. See PDFium's
37 // FPDF_SetPrintMode() for details.
43 // Values 4 and 5 are similar to `kPostScript2` and `kPostScript3`, but are
44 // not intended for use in sandboxed environments like Chromium's.
45 kEmfWithReducedRasterization = 6,
46 kPostScript3WithType42Fonts = 7,
47 // Value 8 is similar to `kPostScript3WithType42Fonts`, but is not intended
48 // for use in sandboxed environments like Chromium's.
51 // `pdf_buffer` is the buffer that contains the entire PDF document to be
53 // `page_index` is the 0-based index of the page to be rendered.
54 // `dc` is the device context to render into.
55 // `dpi_x` and `dpi_y` is the resolution.
56 // `bounds_origin_x`, `bounds_origin_y`, `bounds_width` and `bounds_height`
57 // specify a bounds rectangle within the DC in which to render the PDF
59 // `fit_to_bounds` specifies whether the output should be shrunk to fit the
60 // supplied bounds if the page size is larger than the bounds in any
61 // dimension. If this is false, parts of the PDF page that lie outside
62 // the bounds will be clipped.
63 // `stretch_to_bounds` specifies whether the output should be stretched to fit
64 // the supplied bounds if the page size is smaller than the bounds in any
66 // If both `fit_to_bounds` and `stretch_to_bounds` are true, then
67 // `fit_to_bounds` is honored first.
68 // `keep_aspect_ratio` If any scaling is to be done is true, this flag
69 // specifies whether the original aspect ratio of the page should be
70 // preserved while scaling.
71 // `center_in_bounds` specifies whether the final image (after any scaling is
72 // done) should be centered within the given bounds.
73 // `autorotate` specifies whether the final image should be rotated to match
75 // `use_color` specifies color or grayscale.
76 // Returns false if the document or the page number are not valid.
77 bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
87 bool stretch_to_bounds,
88 bool keep_aspect_ratio,
89 bool center_in_bounds,
93 void SetPDFUsePrintMode(int mode);
94 #endif // BUILDFLAG(IS_WIN)
96 // `page_count` and `max_page_width` are optional and can be NULL.
97 // Returns false if the document is not valid.
98 bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
100 float* max_page_width);
102 // Whether the PDF is Tagged (see 10.7 "Tagged PDF" in PDF Reference 1.7).
103 // Returns true if it's a tagged (accessible) PDF, false if it's a valid
104 // PDF but untagged, and nullopt if the PDF can't be parsed.
105 absl::optional<bool> IsPDFDocTagged(base::span<const uint8_t> pdf_buffer);
107 // Given a tagged PDF (see IsPDFDocTagged, above), return the portion of
108 // the structure tree for a given page as a hierarchical tree of base::Values.
109 base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
112 // Gets the dimensions of a specific page in a document.
113 // `pdf_buffer` is the buffer that contains the entire PDF document to be
115 // `page_index` is the page number that the function will get the dimensions of.
116 // Returns the size of the page in points, or nullopt if the document or the
117 // page number are not valid.
118 absl::optional<gfx::SizeF> GetPDFPageSizeByIndex(
119 base::span<const uint8_t> pdf_buffer,
122 enum class RenderDeviceType {
127 struct RenderOptions {
128 // Whether the output should be stretched to fit the supplied bitmap.
129 bool stretch_to_bounds;
130 // If any scaling is needed, whether the original aspect ratio of the page is
131 // preserved while scaling.
132 bool keep_aspect_ratio;
133 // Whether the final image should be rotated to match the output bound.
135 // Specifies color or grayscale.
137 // What type of device to render for.
138 RenderDeviceType render_device_type;
141 // Renders PDF page into 4-byte per pixel BGRA color bitmap.
142 // `pdf_buffer` is the buffer that contains the entire PDF document to be
144 // `page_index` is the 0-based index of the page to be rendered.
145 // `bitmap_buffer` is the output buffer for bitmap.
146 // `bitmap_size` is the size of the output bitmap.
147 // `dpi` is the 2D resolution.
148 // `options` is the options to render with.
149 // Returns false if the document or the page number are not valid.
150 bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
153 const gfx::Size& bitmap_size,
154 const gfx::Size& dpi,
155 const RenderOptions& options);
157 // Convert multiple PDF pages into a N-up PDF.
158 // `input_buffers` is the vector of buffers with each buffer contains a PDF.
159 // If any of the PDFs contains multiple pages, only the first page of the
161 // `pages_per_sheet` is the number of pages to put on one sheet.
162 // `page_size` is the output page size, measured in PDF "user space" units.
163 // `printable_area` is the output page printable area, measured in PDF
164 // "user space" units. Should be smaller than `page_size`.
166 // `page_size` is the print media size. The page size of the output N-up PDF is
167 // determined by the `pages_per_sheet`, the orientation of the PDF pages
168 // contained in the `input_buffers`, and the media page size `page_size`. For
169 // example, when `page_size` = 512x792, `pages_per_sheet` = 2, and the
170 // orientation of `input_buffers` = portrait, the output N-up PDF will be
172 // See printing::NupParameters for more details on how the output page
173 // orientation is determined, to understand why `page_size` may be swapped in
175 std::vector<uint8_t> ConvertPdfPagesToNupPdf(
176 std::vector<base::span<const uint8_t>> input_buffers,
177 size_t pages_per_sheet,
178 const gfx::Size& page_size,
179 const gfx::Rect& printable_area);
181 // Convert a PDF document to a N-up PDF document.
182 // `input_buffer` is the buffer that contains the entire PDF document to be
183 // converted to a N-up PDF document.
184 // `pages_per_sheet` is the number of pages to put on one sheet.
185 // `page_size` is the output page size, measured in PDF "user space" units.
186 // `printable_area` is the output page printable area, measured in PDF
187 // "user space" units. Should be smaller than `page_size`.
189 // Refer to the description of ConvertPdfPagesToNupPdf to understand how the
190 // output page size will be calculated.
191 // The algorithm used to determine the output page size is the same.
192 std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
193 base::span<const uint8_t> input_buffer,
194 size_t pages_per_sheet,
195 const gfx::Size& page_size,
196 const gfx::Rect& printable_area);
198 } // namespace chrome_pdf