pdf.h 8.1 KB

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