* Search for %PDF...%%EOF in a buffer and extract the PDF bytes.
(buf: Uint8Array)
| 169 | * Search for %PDF...%%EOF in a buffer and extract the PDF bytes. |
| 170 | */ |
| 171 | function extractPdfFromBuffer(buf: Uint8Array): Uint8Array | null { |
| 172 | const pdfStart = findSequence(buf, PDF_HEADER) |
| 173 | if (pdfStart === -1) return null |
| 174 | |
| 175 | // Search for %%EOF from the end (PDF may have multiple %%EOF; take the last one) |
| 176 | let pdfEnd = -1 |
| 177 | for (let i = buf.length - PDF_EOF.length; i >= pdfStart; i--) { |
| 178 | if (matchesAt(buf, i, PDF_EOF)) { |
| 179 | pdfEnd = i + PDF_EOF.length |
| 180 | break |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (pdfEnd === -1) { |
| 185 | // No %%EOF found — take everything from %PDF to end of buffer |
| 186 | pdfEnd = buf.length |
| 187 | } |
| 188 | |
| 189 | return buf.slice(pdfStart, pdfEnd) |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Parse a STRETCHDIBITS record and extract the bitmap as ImageData. |
no test coverage detected