mozilla/pdf.js · error · Error
Invalid SVG dimensions
Error message
Invalid SVG dimensions
What it means
Thrown by BaseSVGFactory.create when width or height is less than or equal to zero. The factory builds an SVG with a viewBox and explicit pixel dimensions, which require strictly positive sizes. A non-positive size is treated as invalid caller input rather than an empty graphic.
Source
Thrown at src/display/svg_factory.js:30
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { SVG_NS, unreachable } from "../shared/util.js";
class BaseSVGFactory {
constructor() {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseSVGFactory
) {
unreachable("Cannot initialize BaseSVGFactory.");
}
}
create(width, height, skipDimensions = false) {
if (width <= 0 || height <= 0) {
throw new Error("Invalid SVG dimensions");
}
const svg = this._createSVG("svg:svg");
svg.setAttribute("version", "1.1");
if (!skipDimensions) {
svg.setAttribute("width", `${width}px`);
svg.setAttribute("height", `${height}px`);
}
svg.setAttribute("preserveAspectRatio", "none");
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
return svg;
}
createElement(type) {
if (typeof type !== "string") {
throw new Error("Invalid SVG element type");View on GitHub (pinned to 5903d58d58)
Solutions
- Validate width and height are positive numbers before calling create().
- Guard upstream: skip SVG generation for zero-area content instead of forcing it.
- Default to a small positive size only if a non-empty graphic is genuinely required.
- Trace where the dimension originates to fix the root cause.
Example fix
// before
const svg = factory.create(crop.width, crop.height);
// after
if (crop.width > 0 && crop.height > 0) {
const svg = factory.create(crop.width, crop.height);
} Defensive patterns
Strategy: validation
Validate before calling
if (!(Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0)) {
throw new Error('width and height must be positive finite numbers');
} Type guard
function areValidSvgDimensions(width, height) {
return Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0;
} Prevention
- Skip SVG creation for zero-area content rather than forcing it.
- Trace dimension sources (crops, bboxes) to their origin when they go zero/negative.
- Always validate positive finite numbers before calling create().
- Avoid passing undefined, which silently fails the > 0 check.
When it happens
Trigger: factory.create(width, height) is called with width<=0 or height<=0.
Common situations: Dimensions computed from a zero-area page crop, an empty text/annotation bbox, or a fallback when a size could not be determined; passing undefined which coerces via comparison.
Related errors
- Invalid image width: ${width} or height: ${height}
- PageViewport: Invalid rotation, must be a multiple of 90 deg
- No "textContentSource" parameter specified.
- Invalid `workerPort` type.
- Invalid `workerSrc` type.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/8134bf988ab49616.
Report an issue: GitHub.