MCPcopy Create free account
hub / github.com/LibPDF-js/core / embedJpeg

Method embedJpeg

src/api/pdf.ts:2130–2150  ·  view source on GitHub ↗

* Embed a JPEG image into the document. * * JPEG images are embedded directly using DCTDecode filter, * which means the JPEG data is stored as-is without re-encoding. * * @param bytes - JPEG file bytes * @returns PDFImage that can be drawn with page.drawImage() * @throws {Error}

(bytes: Uint8Array)

Source from the content-addressed store, hash-verified

2128 * ```
2129 */
2130 embedJpeg(bytes: Uint8Array): PDFImage {
2131 const info = parseJpegHeader(bytes);
2132
2133 // Create XObject image stream with DCTDecode filter
2134 const stream = PdfStream.fromDict(
2135 {
2136 Type: PdfName.of("XObject"),
2137 Subtype: PdfName.of("Image"),
2138 Width: PdfNumber.of(info.width),
2139 Height: PdfNumber.of(info.height),
2140 ColorSpace: PdfName.of(info.colorSpace),
2141 BitsPerComponent: PdfNumber.of(8),
2142 Filter: PdfName.of("DCTDecode"),
2143 },
2144 bytes,
2145 );
2146
2147 const ref = this.register(stream);
2148
2149 return new PDFImage(ref, info.width, info.height);
2150 }
2151
2152 /**
2153 * Embed a PNG image into the document.

Callers 3

embedImageMethod · 0.95
factory.test.tsFile · 0.80
mainFunction · 0.80

Calls 4

registerMethod · 0.95
parseJpegHeaderFunction · 0.90
fromDictMethod · 0.80
ofMethod · 0.45

Tested by

no test coverage detected