Line data Source code
1 : // Copyright 2026 The Authors.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // https://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : /// The intrinsic size of a PDF page, measured in PDF user units (points).
16 : ///
17 : /// One PDF user unit equals 1/72 inch. This is a storage-level measurement
18 : /// independent of any rendering resolution. Use [sizeForDpi] to convert to
19 : /// pixel dimensions at a target DPI for rendering.
20 : ///
21 : /// ## Example
22 : ///
23 : /// ```dart
24 : /// final size = await doc.getPageSize(0);
25 : /// // Convert to pixels at 150 DPI:
26 : /// final pixelSize = size.sizeForDpi(150);
27 : /// final image = await doc.renderPageToBytes(
28 : /// 0,
29 : /// pixelSize.width.round(),
30 : /// pixelSize.height.round(),
31 : /// );
32 : /// ```
33 : class PdfPageSize {
34 : /// Creates a [PdfPageSize] with the given [widthPt] and [heightPt] in points.
35 : ///
36 : /// Both values must be positive. A value of zero or negative indicates a
37 : /// malformed page in the PDF; callers should guard against this before
38 : /// passing dimensions to [PdfDocument.renderPageToBytes].
39 4 : const PdfPageSize({required this.widthPt, required this.heightPt});
40 :
41 : /// The page width in PDF user units (points, 1/72 inch).
42 : final double widthPt;
43 :
44 : /// The page height in PDF user units (points, 1/72 inch).
45 : final double heightPt;
46 :
47 : /// The aspect ratio of the page (width / height).
48 : ///
49 : /// Returns `1.0` when [heightPt] is zero to avoid division-by-zero on
50 : /// malformed pages. Callers should still guard against zero-sized pages
51 : /// before rendering.
52 12 : double get aspectRatio => heightPt > 0 ? widthPt / heightPt : 1.0;
53 :
54 : /// Returns the page size in pixels at the given [dpi] as a named record.
55 : ///
56 : /// Multiply [widthPt] and [heightPt] (both in points, 1/72 inch) by
57 : /// [dpi] / 72 to obtain pixel dimensions suitable for passing to
58 : /// [PdfDocument.renderPageToBytes].
59 : ///
60 : /// Example: an A4 page (595 × 842 pt) at 150 DPI yields
61 : /// approximately 1239 × 1754 pixels.
62 : ///
63 : /// [dpi] must be positive. Returns `(width: 0.0, height: 0.0)` when [dpi]
64 : /// is zero or negative.
65 2 : ({double width, double height}) sizeForDpi(double dpi) {
66 2 : if (dpi <= 0) return (width: 0.0, height: 0.0);
67 2 : final scale = dpi / 72.0;
68 8 : return (width: widthPt * scale, height: heightPt * scale);
69 : }
70 :
71 1 : @override
72 3 : String toString() => 'PdfPageSize(widthPt: $widthPt, heightPt: $heightPt)';
73 :
74 2 : @override
75 : bool operator ==(Object other) =>
76 : identical(this, other) ||
77 1 : other is PdfPageSize &&
78 3 : other.widthPt == widthPt &&
79 3 : other.heightPt == heightPt;
80 :
81 1 : @override
82 3 : int get hashCode => Object.hash(widthPt, heightPt);
83 : }
|