{"record":{"id":"44883d81eda1f0d4","repo":"docusealco/docuseal","slug":"failed-to-create-bitmap-fpdfbitmap-create-returne","errorCode":null,"errorMessage":"Failed to create bitmap (FPDFBitmap_Create returned NULL)","messagePattern":"Failed to create bitmap \\(FPDFBitmap_Create returned NULL\\)","errorType":"exception","errorClass":"Pdfium::PdfiumError","httpStatus":null,"severity":"error","filePath":"lib/pdfium.rb","lineNumber":1032,"sourceCode":"\n    def ensure_not_closed!\n      raise PdfiumError, 'Page is closed.' if closed?\n\n      @document.ensure_not_closed!\n    end\n\n    def render_to_bitmap(width: nil, height: nil, scale: nil, background_color: 0xFFFFFFFF,\n                         flags: FPDF_ANNOT | FPDF_LCD_TEXT | FPDF_NO_NATIVETEXT | FPDF_REVERSE_BYTE_ORDER)\n      ensure_not_closed!\n\n      render_width, render_height = calculate_render_dimensions(width, height, scale)\n\n      bitmap_ptr = Pdfium.FPDFBitmap_Create(render_width, render_height, 1)\n\n      if bitmap_ptr.null?\n        Pdfium.check_last_error('Failed to create bitmap (potential pre-existing error)')\n\n        raise PdfiumError, 'Failed to create bitmap (FPDFBitmap_Create returned NULL)'\n      end\n\n      Pdfium.FPDFBitmap_FillRect(bitmap_ptr, 0, 0, render_width, render_height, background_color)\n\n      Pdfium.FPDF_RenderPageBitmap(bitmap_ptr, page_ptr, 0, 0, render_width, render_height, 0, flags)\n\n      unless form_handle.null?\n        Pdfium.FPDF_FFLDraw(form_handle, bitmap_ptr, page_ptr, 0, 0, render_width, render_height, 0, flags)\n      end\n\n      buffer_ptr = Pdfium.FPDFBitmap_GetBuffer(bitmap_ptr)\n      stride = Pdfium.FPDFBitmap_GetStride(bitmap_ptr)\n\n      bitmap_data = buffer_ptr.read_bytes(stride * render_height)\n\n      [bitmap_data, render_width, render_height]\n    ensure\n      Pdfium.FPDFBitmap_Destroy(bitmap_ptr) if bitmap_ptr && !bitmap_ptr.null?","sourceCodeStart":1014,"sourceCodeEnd":1050,"githubUrl":"https://github.com/docusealco/docuseal/blob/004a22c1c88109c7ba0b567df011a8cb13894001/lib/pdfium.rb#L1014-L1050","documentation":"Raised by Page#render_to_bitmap when FPDFBitmap_Create returns NULL for the computed render dimensions. This is a native allocation failure: the bitmap consumes roughly width*height*4 bytes, so oversized dimensions (large scale values, explicit width/height, or poster-sized pages) exhaust the memory budget. check_last_error runs first with the 'potential pre-existing error' context, but PDFium records no error code for bitmap allocation, so this plain raise is what you see.","triggerScenarios":"render_to_bitmap(scale: 8) on a large page; passing width: 20000 explicitly; rendering A0/architecture drawings at default scale; rendering while the process is already near its memory cap.","commonSituations":"High-DPI thumbnail generation; zoomed-view rendering features; unvalidated user-supplied render dimensions (a denial-of-service vector); low container memory limits.","solutions":["Cap render dimensions: pass explicit width/height clamped to roughly 3000-5000px instead of an unbounded scale","Compute the target pixel count from page.width/height first and skip or downscale when it exceeds a budget","Free other documents and bitmaps before large renders; raise the container memory limit","Add Pdfium::FPDF_RENDER_LIMITEDIMAGECACHE to the flags to bound pdfium's image cache"],"exampleFix":"# before\nbitmap = page.render_to_bitmap(scale: 8)\n\n# after\nMAX = 3000\nscale = [8, (MAX / page.width).to_f, (MAX / page.height).to_f].min\nbitmap = page.render_to_bitmap(scale: scale)","handlingStrategy":"validation","validationCode":"MAX_SIDE = 3000\nMAX_PIXELS = 8_000_000\n\ndef clamped_scale(page, desired)\n  s = [(desired || 1).to_f, 0.1].max\n  s = [s, MAX_SIDE / page.width, MAX_SIDE / page.height].min\n  s = [s, Math.sqrt(MAX_PIXELS / (page.width * page.height))].min\n  [s, 0.1].max\nend","typeGuard":null,"tryCatchPattern":"begin\n  page.render_to_bitmap(scale: scale)\nrescue Pdfium::PdfiumError => e\n  raise unless e.message.include?('Failed to create bitmap')\n  page.render_to_bitmap(width: 1500, height: nil) # degrade gracefully\nend","preventionTips":["Never pass user-supplied width/height/scale unchecked into render_to_bitmap","Budget pixels: width*height*4 bytes of native memory per render","Add FPDF_RENDER_LIMITEDIMAGECACHE to render flags for cache bounding"],"tags":["pdfium","rendering","out-of-memory","bitmap","ruby"],"backgroundTag":"memory-allocation-failure","analyzedSha":"004a22c1c88109c7ba0b567df011a8cb13894001","analyzedAt":"2026-08-21T13:38:23.343Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}