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"
13 #include "third_party/abseil-cpp/absl/types/optional.h"
25 namespace chrome_pdf {
27 #if BUILDFLAG(IS_CHROMEOS)
28 // Create a flattened PDF document from an existing PDF document.
29 // `input_buffer` is the buffer that contains the entire PDF document to be
31 std::vector<uint8_t> CreateFlattenedPdf(base::span<const uint8_t> input_buffer);
32 #endif // BUILDFLAG(IS_CHROMEOS)
35 // Printing modes - type to convert PDF to for printing. See PDFium's
36 // FPDF_SetPrintMode() for details.
42 // Values 4 and 5 are similar to `kPostScript2` and `kPostScript3`, but are
43 // not intended for use in sandboxed environments like Chromium's.
44 kEmfWithReducedRasterization = 6,
45 kPostScript3WithType42Fonts = 7,
46 // Value 8 is similar to `kPostScript3WithType42Fonts`, but is not intended
47 // for use in sandboxed environments like Chromium's.
50 // `pdf_buffer` is the buffer that contains the entire PDF document to be
52 // `page_index` is the 0-based index of the page to be rendered.
53 // `dc` is the device context to render into.
54 // `dpi_x` and `dpi_y` is the resolution.
55 // `bounds_origin_x`, `bounds_origin_y`, `bounds_width` and `bounds_height`
56 // specify a bounds rectangle within the DC in which to render the PDF
58 // `fit_to_bounds` specifies whether the output should be shrunk to fit the
59 // supplied bounds if the page size is larger than the bounds in any
60 // dimension. If this is false, parts of the PDF page that lie outside
61 // the bounds will be clipped.
62 // `stretch_to_bounds` specifies whether the output should be stretched to fit
63 // the supplied bounds if the page size is smaller than the bounds in any
65 // If both `fit_to_bounds` and `stretch_to_bounds` are true, then
66 // `fit_to_bounds` is honored first.
67 // `keep_aspect_ratio` If any scaling is to be done is true, this flag
68 // specifies whether the original aspect ratio of the page should be
69 // preserved while scaling.
70 // `center_in_bounds` specifies whether the final image (after any scaling is
71 // done) should be centered within the given bounds.
72 // `autorotate` specifies whether the final image should be rotated to match
74 // `use_color` specifies color or grayscale.
75 // Returns false if the document or the page number are not valid.
76 bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
86 bool stretch_to_bounds,
87 bool keep_aspect_ratio,
88 bool center_in_bounds,
92 void SetPDFUsePrintMode(int mode);
93 #endif // BUILDFLAG(IS_WIN)
95 // `page_count` and `max_page_width` are optional and can be NULL.
96 // Returns false if the document is not valid.
97 bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
99 float* max_page_width);
101 // Whether the PDF is Tagged (see 10.7 "Tagged PDF" in PDF Reference 1.7).
102 // Returns true if it's a tagged (accessible) PDF, false if it's a valid
103 // PDF but untagged, and nullopt if the PDF can't be parsed.
104 absl::optional<bool> IsPDFDocTagged(base::span<const uint8_t> pdf_buffer);
106 // Given a tagged PDF (see IsPDFDocTagged, above), return the portion of
107 // the structure tree for a given page as a hierarchical tree of base::Values.
108 base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
111 // Gets the dimensions of a specific page in a document.
112 // `pdf_buffer` is the buffer that contains the entire PDF document to be
114 // `page_index` is the page number that the function will get the dimensions of.
115 // Returns the size of the page in points, or nullopt if the document or the
116 // page number are not valid.
117 absl::optional<gfx::SizeF> GetPDFPageSizeByIndex(
118 base::span<const uint8_t> pdf_buffer,
121 enum class RenderDeviceType {
126 struct RenderOptions {
127 // Whether the output should be stretched to fit the supplied bitmap.
128 bool stretch_to_bounds;
129 // If any scaling is needed, whether the original aspect ratio of the page is
130 // preserved while scaling.
131 bool keep_aspect_ratio;
132 // Whether the final image should be rotated to match the output bound.
134 // Specifies color or grayscale.
136 // What type of device to render for.
137 RenderDeviceType render_device_type;
140 // Renders PDF page into 4-byte per pixel BGRA color bitmap.
141 // `pdf_buffer` is the buffer that contains the entire PDF document to be
143 // `page_index` is the 0-based index of the page to be rendered.
144 // `bitmap_buffer` is the output buffer for bitmap.
145 // `bitmap_size` is the size of the output bitmap.
146 // `dpi` is the 2D resolution.
147 // `options` is the options to render with.
148 // Returns false if the document or the page number are not valid.
149 bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
152 const gfx::Size& bitmap_size,
153 const gfx::Size& dpi,
154 const RenderOptions& options);
156 // Convert multiple PDF pages into a N-up PDF.
157 // `input_buffers` is the vector of buffers with each buffer contains a PDF.
158 // If any of the PDFs contains multiple pages, only the first page of the
160 // `pages_per_sheet` is the number of pages to put on one sheet.
161 // `page_size` is the output page size, measured in PDF "user space" units.
162 // `printable_area` is the output page printable area, measured in PDF
163 // "user space" units. Should be smaller than `page_size`.
165 // `page_size` is the print media size. The page size of the output N-up PDF is
166 // determined by the `pages_per_sheet`, the orientation of the PDF pages
167 // contained in the `input_buffers`, and the media page size `page_size`. For
168 // example, when `page_size` = 512x792, `pages_per_sheet` = 2, and the
169 // orientation of `input_buffers` = portrait, the output N-up PDF will be
171 // See printing::NupParameters for more details on how the output page
172 // orientation is determined, to understand why `page_size` may be swapped in
174 std::vector<uint8_t> ConvertPdfPagesToNupPdf(
175 std::vector<base::span<const uint8_t>> input_buffers,
176 size_t pages_per_sheet,
177 const gfx::Size& page_size,
178 const gfx::Rect& printable_area);
180 // Convert a PDF document to a N-up PDF document.
181 // `input_buffer` is the buffer that contains the entire PDF document to be
182 // converted to a N-up PDF document.
183 // `pages_per_sheet` is the number of pages to put on one sheet.
184 // `page_size` is the output page size, measured in PDF "user space" units.
185 // `printable_area` is the output page printable area, measured in PDF
186 // "user space" units. Should be smaller than `page_size`.
188 // Refer to the description of ConvertPdfPagesToNupPdf to understand how the
189 // output page size will be calculated.
190 // The algorithm used to determine the output page size is the same.
191 std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
192 base::span<const uint8_t> input_buffer,
193 size_t pages_per_sheet,
194 const gfx::Size& page_size,
195 const gfx::Rect& printable_area);
197 } // namespace chrome_pdf