{"_id":"@danchitnis/svg-to-pdf","name":"@danchitnis/svg-to-pdf","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@danchitnis/svg-to-pdf","version":"1.0.0","description":"convert svg to pdf","homepage":"https://github.com/danchitnis/svg-to-pdf#readme","bugs":{"url":"https://github.com/danchitnis/svg-to-pdf/issues"},"repository":{"type":"git","url":"git+https://github.com/danchitnis/svg-to-pdf.git"},"license":"MIT","author":{"name":"Danial Chitnis"},"type":"module","exports":{".":{"types":"./dist/index.d.ts","browser":"./dist/svg-to-pdf.mjs","import":"./dist/svg-to-pdf.mjs","require":"./dist/svg-to-pdf.cjs"}},"main":"./dist/svg-to-pdf.cjs","browser":"./dist/svg-to-pdf.mjs","types":"./dist/index.d.ts","directories":{"example":"examples","test":"test"},"scripts":{"build":"vite build && tsc --project tsconfig.build.json","check":"tsc --noEmit & eslint .","test":"npx tsx test/basic-usage.test.ts","dev":"npx tsx test/basic-usage.test.ts","prepublishOnly":"npm run build"},"dependencies":{"pdf-lib":"^1.17.1"},"devDependencies":{"@eslint/js":"^9.39.2","@types/jsdom":"^27.0.0","@types/node":"^25.2.3","eslint":"^9.39.2","globals":"^17.3.0","jsdom":"^28.1.0","ts-node":"^10.9.2","tsx":"^4.21.0","typescript":"^5.9.3","typescript-eslint":"^8.56.0","vite":"^7.3.1"},"module":"./dist/svg-to-pdf.mjs","gitHead":"48313ec68e709b3accbf0353af0ac6ab9e5ea4c9","_id":"@danchitnis/svg-to-pdf@1.0.0","_nodeVersion":"22.20.0","_npmVersion":"11.6.4","dist":{"integrity":"sha512-au/Xd0FTx7y0lBzj6U58V14v+eU8QQoy8a4WBUtKFrvcLhq/PLMv6L+cIYhVDPqy++7fUjldyEGBsb/cupjIKg==","shasum":"d8bd3f35e52ee1e1e8a48f631c4ffc63d735f3b6","tarball":"https://registry.npmjs.org/@danchitnis/svg-to-pdf/-/svg-to-pdf-1.0.0.tgz","fileCount":16,"unpackedSize":70631,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIGOnJ4nCzqMJe2DWrMTSl8WrQjh7AAYS2vBx/gYvr6QVAiEA2MBgOqtY2zF5nC98D9riI2PGBYvQkMOQHSJcdw1u74M="}]},"_npmUser":{"name":"danchitnis","email":"danchitnis@gmail.com"},"maintainers":[{"name":"danchitnis","email":"danchitnis@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/svg-to-pdf_1.0.0_1771275932222_0.8317540619284309"},"_hasShrinkwrap":false}},"time":{"created":"2026-02-16T21:05:32.157Z","1.0.0":"2026-02-16T21:05:32.430Z","modified":"2026-02-16T21:05:32.597Z"},"maintainers":[{"name":"danchitnis","email":"danchitnis@gmail.com"}],"description":"convert svg to pdf","homepage":"https://github.com/danchitnis/svg-to-pdf#readme","repository":{"type":"git","url":"git+https://github.com/danchitnis/svg-to-pdf.git"},"author":{"name":"Danial Chitnis"},"bugs":{"url":"https://github.com/danchitnis/svg-to-pdf/issues"},"license":"MIT","readme":"# @danchitnis/svg-to-pdf\n\nA lightweight, flexible, and platform-agnostic library to convert SVG to PDF using [pdf-lib](https://github.com/Hopding/pdf-lib).\n\n## Features\n\n- **Platform Agnostic**: Works in both Node.js and the Browser.\n- **Flexible Layout**: specific paper sizes (A4, Letter, etc.), custom dimensions, or content-scaling.\n- **SVG Support**:\n  - Basic Shapes: `rect`, `circle`, `ellipse`, `line`, `polyline`, `polygon`.\n  - Paths: Comprehensive `d` attribute parsing.\n  - Transforms: `translate`, `scale`, `rotate`, `matrix` support.\n  - Groups (`<g>`) and hierarchy.\n  - Styles: Basic styling via attributes.\n  - Text: Basic text rendering (standard fonts).\n  - ViewBox: Full `viewBox` support with aspect ratio handling.\n\n## Installation\n\n```bash\nnpm install @danchitnis/svg-to-pdf\n```\n\n## Usage\n\nThis library operates on a simplified, platform-neutral JSON structure (`SimpleSvgElement`) instead of directly on DOM nodes. This design allows it to run in complete isolation from browser APIs (perfect for Node.js) or within a browser environment.\n\nYou need to convert your SVG (whether a string or a DOM element) into this simple structure before passing it to the converter.\n\n### Node.js Example\n\nIn Node.js, you can use `jsdom` to parse the SVG string.\n\n1. Install dependencies:\n   ```bash\n   npm install jsdom\n   npm install --save-dev @types/jsdom\n   ```\n\n2. Create a converter script:\n\n   ```typescript\n   import { convertSvgToPdfFromSimpleStructure, SimpleSvgElement } from \"@danchitnis/svg-to-pdf\";\n   import { JSDOM } from \"jsdom\";\n   import * as fs from \"fs/promises\";\n\n   // 1. Define a helper to convert JSDOM/DOM elements to SimpleSvgElement\n   function adaptDomToSimpleSvgStructure(element: Element): SimpleSvgElement {\n     const attributes: { [key: string]: string } = {};\n     for (const attr of Array.from(element.attributes)) {\n       attributes[attr.name] = attr.value;\n     }\n\n     const children: SimpleSvgElement[] = [];\n     for (const childNode of Array.from(element.children)) {\n       children.push(adaptDomToSimpleSvgStructure(childNode as Element));\n     }\n\n     return {\n       tagName: element.tagName,\n       attributes,\n       children,\n       textContent: element.textContent,\n       getAttribute: (name) => element.getAttribute(name),\n     };\n   }\n\n   async function main() {\n     // 2. Read and Parse SVG\n     const svgContent = await fs.readFile(\"input.svg\", \"utf-8\");\n     const dom = new JSDOM(svgContent, { contentType: \"image/svg+xml\" });\n     const svgElement = dom.window.document.documentElement;\n\n     if (!svgElement || svgElement.tagName.toLowerCase() !== \"svg\") {\n         throw new Error(\"Invalid SVG\");\n     }\n\n     // 3. Adapt to Simple Structure\n     const simpleSvg = adaptDomToSimpleSvgStructure(svgElement);\n\n     // 4. Convert to PDF\n     const pdfBytes = await convertSvgToPdfFromSimpleStructure(simpleSvg, {\n       paperSize: \"A4\", // or 'Letter', or { width: 210, height: 297 }\n       backgroundColor: \"white\",\n     });\n\n     // 5. Save PDF\n     await fs.writeFile(\"output.pdf\", pdfBytes);\n     console.log(\"PDF created!\");\n   }\n\n   main();\n   ```\n\n### Browser Example\n\nIn the browser, you can directly interact with the DOM elements.\n\n```typescript\nimport { convertSvgToPdfFromSimpleStructure, SimpleSvgElement } from \"@danchitnis/svg-to-pdf\";\n\n// Same helper function as above\nfunction adaptDomToSimpleSvgStructure(element: Element): SimpleSvgElement {\n  // ... (see Node.js example) ...\n}\n\nasync function exportSvgToPdf() {\n  const svgElement = document.querySelector(\"#my-svg-chart\");\n  const simpleSvg = adaptDomToSimpleSvgStructure(svgElement);\n\n  const pdfBytes = await convertSvgToPdfFromSimpleStructure(simpleSvg, {\n     multiplier: 2, // Scale up content 2x\n     backgroundColor: \"#ffffff\"\n  });\n\n  // Download the PDF file (example utility)\n  const blob = new Blob([pdfBytes], { type: \"application/pdf\" });\n  const link = document.createElement(\"a\");\n  link.href = URL.createObjectURL(blob);\n  link.download = \"chart.pdf\";\n  link.click();\n}\n```\n\n## API\n\n### `convertSvgToPdfFromSimpleStructure(svgElement, options)`\n\n**Parameters:**\n\n- `svgElement`: `SimpleSvgElement` - The root SVG element in specific JSON format.\n- `options`: `SvgToPdfOptions` - Configuration for the output PDF.\n\n**Returns:** `Promise<Uint8Array>` - The generated PDF as a byte array.\n\n### `SvgToPdfOptions`\n\n| Option | Type | Description |\n| search | type | description |\n|---|---|---|\n| `width` | `number` | Override output width (in points). |\n| `height` | `number` | Override output height (in points). |\n| `scale` | `number` | Apply a global scale factor to the content. |\n| `multiplier` | `number` | Simple scaling: Output Size = SVG Size * Multiplier. |\n| `paperSize` | `string` \\| `{ w, h }` | Fit content to standard paper (e.g., `'A4'`, `'Letter'`) or custom dimensions (mm). |\n| `backgroundColor` | `string` | Background color name or hex code (e.g., `'white'`, `'#f0f0f0'`). |\n\n### Supported Paper Sizes\n\n- A3, A4, A5\n- Letter, Legal, Tabloid\n\n## License\n\nISC\n","readmeFilename":"README.md","_rev":"1-f93ed68fc1e08f30db9504289db0ce48"}