WFDASH-WEBAPP/scripts/dev/debug-png-variants.tsx

38 lines
1.2 KiB
TypeScript
Raw Normal View History

/* eslint-disable */
import * as React from "react";
import { Document, Page, Image, pdf } from "@react-pdf/renderer";
import fs from "fs";
async function main() {
const png = fs.readFileSync("/tmp/debug-logo.png");
// Test all 4 src variants
const variants: Array<[string, any]> = [
["Blob (image/png)", new Blob([new Uint8Array(png)], { type: "image/png" })],
["Uint8Array", new Uint8Array(png)],
["Buffer (Node)", png],
["base64 string (NO data: prefix)", png.toString("base64")],
];
for (const [label, src] of variants) {
try {
const inst = pdf(
<Document><Page size="A4" style={{padding: 40}}>
<Image src={src} style={{ width: 110, height: 68 }} />
</Page></Document>
);
const out = Buffer.from(await inst.toString(), "binary");
console.log(`[${label}] size: ${out.length}`);
// Find image stream
const streamIdx = out.indexOf(Buffer.from("stream\n"));
if (streamIdx >= 0) {
const head = out.subarray(streamIdx + 7, streamIdx + 30);
console.log(` stream head: ${head.toString("hex")}`);
}
} catch (e) {
console.log(`[${label}] FAILED: ${e.message}`);
}
}
}
main().catch(e => { console.error("FAIL:", e); process.exit(1); });