parallax/jsPDF · error · Error

Invalid BMP File

Error message

Invalid BMP File

What it means

Thrown by the BMPDecoder constructor when the first two bytes of the buffer do not form a recognized BMP signature. Valid signatures are BM, BA, CI, CP, IC, PT (the standard OS/2 and Windows BMP magic markers). Any other leading bytes indicate the data is not a BMP, is truncated, or is a different format.

Source

Thrown at src/libs/BMPDecoder.js:20

 * @author shaozilee
 *
 * Bmp format decoder,support 1bit 4bit 8bit 24bit bmp
 *
 */

import { console } from "./console.js";

function BmpDecoder(buffer, is_with_alpha) {
  this.pos = 0;
  this.buffer = buffer;
  this.datav = new DataView(buffer.buffer);
  this.is_with_alpha = !!is_with_alpha;
  this.bottom_up = true;
  this.flag =
    String.fromCharCode(this.buffer[0]) + String.fromCharCode(this.buffer[1]);
  this.pos += 2;
  if (["BM", "BA", "CI", "CP", "IC", "PT"].indexOf(this.flag) === -1)
    throw new Error("Invalid BMP File");
  this.parseHeader();
  this.parseBGR();
}

BmpDecoder.prototype.parseHeader = function() {
  this.fileSize = this.datav.getUint32(this.pos, true);
  this.pos += 4;
  this.reserved = this.datav.getUint32(this.pos, true);
  this.pos += 4;
  this.offset = this.datav.getUint32(this.pos, true);
  this.pos += 4;
  this.headerSize = this.datav.getUint32(this.pos, true);
  this.pos += 4;
  this.width = this.datav.getUint32(this.pos, true);
  this.pos += 4;
  this.height = this.datav.getInt32(this.pos, true);
  this.pos += 4;
  this.planes = this.datav.getUint16(this.pos, true);

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Verify the file is actually a BMP (check magic bytes 0x42 0x4D for 'BM') before decoding.
  2. Use the correct decoder for the actual format (PNGDecoder, JPEG path) or a format-sniffing dispatcher.
  3. Ensure you pass a decoded byte array/Uint8Array, not a base64 string; strip any data-URI prefix.

Example fix

// before
var bmp = new BmpDecoder(someBuffer);
// after
if (someBuffer[0] === 0x42 && someBuffer[1] === 0x4D) {
  var bmp = new BmpDecoder(someBuffer);
}
Defensive patterns

Strategy: type-guard

Validate before calling

function isBmp(buf){ return buf && buf.length >= 2 && ['BM','BA','CI','CP','IC','PT'].indexOf(String.fromCharCode(buf[0],buf[1])) !== -1; }
if (isBmp(buffer)) new BmpDecoder(buffer);

Type guard

function isBmpBuffer(buf){ return buf instanceof Uint8Array && buf.length >= 2 && buf[0]===0x42 && buf[1]===0x4D; }

Try / catch

try { var bmp = new BmpDecoder(buffer); } catch (e) { if (/Invalid BMP File/.test(e.message)) { /* route to other decoder */ } else throw e; }

Prevention

When it happens

Trigger: Passing a PNG/JPEG/WebP buffer to BmpDecoder; passing a base64 string instead of a decoded byte array; buffer that is shorter than 2 bytes; byte order swapped or offset misalignment before decoding.

Common situations: Generic image-upload handler that routes all images through BmpDecoder; mislabelled file extension; forgetting to strip a data-URI prefix before slicing the byte buffer.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/3551402a40701b62. Report an issue: GitHub.