SVG2PDF Pro – Convert Vector to PDF
/* — CSS SEPARATION START — */
@import url(‘https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap’);:root {
–primary: #F97316; /* Orange-500 (SVG Logo Color) */
–bg-dark: #0F172A;
}body {
font-family: ‘Inter’, sans-serif;
transition: background-color 0.3s, color 0.3s;
}/* Checkerboard for transparency */
.checkerboard {
background-color: #ffffff;
background-image: linear-gradient(45deg, #e5e7eb 25%, transparent 25%),
linear-gradient(-45deg, #e5e7eb 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #e5e7eb 75%),
linear-gradient(-45deg, transparent 75%, #e5e7eb 75%);
background-size: 20px 20px;
background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}
.dark .checkerboard {
background-color: #1e293b;
background-image: linear-gradient(45deg, #334155 25%, transparent 25%),
linear-gradient(-45deg, #334155 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #334155 75%),
linear-gradient(-45deg, transparent 75%, #334155 75%);
}/* Loading Spinner */
.loader {
border: 3px solid #f3f3f3;
border-radius: 50%;
border-top: 3px solid #F97316;
width: 20px;
height: 20px;
animation: spin 1s linear infinite;
}@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}/* Dropzone Active */
.dropzone-active {
border-color: #F97316 !important;
background-color: rgba(249, 115, 22, 0.05) !important;
transform: scale(1.01);
}/* Dark Mode */
.dark body {
background-color: var(–bg-dark);
color: #F9FAFB;
}
.dark .card {
background-color: #1E293B;
border-color: #334155;
}
.dark input, .dark select {
background-color: #334155;
color: white;
border-color: #475569;
}
/* — CSS SEPARATION END — */
tailwind.config = {
darkMode: ‘class’,
theme: {
extend: {
colors: {
primary: ‘#F97316’,
}
}
}
}
Transform scalable vector graphics into high-resolution PDF documents. Perfect for printing diagrams, logos, and illustrations.
PDF Settings
Page Size
Fit to Image (Custom)
A4 (Standard)
Letter (US)
Orientation
Auto (Detect)
Portrait
Landscape
Margins
No Margin
Standard (20px)
Wide (50px)
Output Quality
Screen (72 DPI)
High (150 DPI)
Print (300 DPI)
Download PDF
Reset
Drop SVG File Here Convert scalable vector to PDF
Select SVG
// — JAVASCRIPT SEPARATION START —// DOM Elements
const dropzone = document.getElementById(‘dropzone’);
const fileInput = document.getElementById(‘fileInput’);
const resultArea = document.getElementById(‘result-area’);
const svgPreview = document.getElementById(‘svg-preview’);
const fileInfo = document.getElementById(‘file-info’);
const btnConvert = document.getElementById(‘btn-convert’);
const btnText = document.getElementById(‘btn-text’);
const btnLoader = document.getElementById(‘btn-loader’);
const canvas = document.getElementById(‘render-canvas’);
const ctx = canvas.getContext(‘2d’);// Controls
const pdfSize = document.getElementById(‘pdf-size’);
const pdfOrientation = document.getElementById(‘pdf-orientation’);
const pdfMargin = document.getElementById(‘pdf-margin’);
const pdfQuality = document.getElementById(‘pdf-quality’);
const orientationGroup = document.getElementById(‘orientation-group’);let currentFile = null;
let uploadedSVGUrl = null;
let isDarkMode = false;// — Event Listeners —
dropzone.addEventListener(‘click’, () => fileInput.click());
dropzone.addEventListener(‘dragover’, (e) => { e.preventDefault(); dropzone.classList.add(‘dropzone-active’); });
dropzone.addEventListener(‘dragleave’, () => dropzone.classList.remove(‘dropzone-active’));
dropzone.addEventListener(‘drop’, (e) => {
e.preventDefault();
dropzone.classList.remove(‘dropzone-active’);
if(e.dataTransfer.files.length) handleFile(e.dataTransfer.files[0]);
});
fileInput.addEventListener(‘change’, (e) => {
if(e.target.files.length) handleFile(e.target.files[0]);
});pdfSize.addEventListener(‘change’, () => {
if(pdfSize.value === ‘fit’) {
orientationGroup.classList.add(‘opacity-50’, ‘pointer-events-none’);
pdfMargin.parentElement.classList.add(‘opacity-50’, ‘pointer-events-none’);
} else {
orientationGroup.classList.remove(‘opacity-50’, ‘pointer-events-none’);
pdfMargin.parentElement.classList.remove(‘opacity-50’, ‘pointer-events-none’);
}
});// — Core Functions —function handleFile(file) {
if (!file.type.includes(‘svg’) && !file.name.endsWith(‘.svg’)) {
alert(‘Please upload a valid SVG file.’);
return;
}
currentFile = file;
const url = URL.createObjectURL(file);
uploadedSVGUrl = url;
svgPreview.src = url;
// Show UI
dropzone.classList.add(‘hidden’);
resultArea.classList.remove(‘hidden’);
btnConvert.disabled = false;// Get original dimensions
const img = new Image();
img.onload = () => {
fileInfo.innerText = `Original Size: ${img.width} x ${img.height} px | ${formatBytes(file.size)}`;
};
img.src = url;
}async function generatePDF() {
btnConvert.disabled = true;
btnText.innerText = ‘Generating…’;
btnLoader.classList.remove(‘hidden’);try {
const { PDFDocument, PageSizes } = PDFLib;
const pdfDoc = await PDFDocument.create();// 1. Render SVG to Canvas at High Res
const img = new Image();
await new Promise((resolve, reject) => {
img.onload = resolve;
img.onerror = reject;
img.src = uploadedSVGUrl;
});const scaleFactor = parseInt(pdfQuality.value); // 1, 2, or 4
const width = img.width * scaleFactor;
const height = img.height * scaleFactor;
canvas.width = width;
canvas.height = height;
ctx.clearRect(0, 0, width, height);
ctx.drawImage(img, 0, 0, width, height);// 2. Convert Canvas to PNG
const pngDataUrl = canvas.toDataURL(‘image/png’);
const pngImageBytes = await fetch(pngDataUrl).then(res => res.arrayBuffer());
const pngImage = await pdfDoc.embedPng(pngImageBytes);// 3. Determine Page Size
const sizeSetting = pdfSize.value;
const orientation = pdfOrientation.value;
const margin = sizeSetting === ‘fit’ ? 0 : parseInt(pdfMargin.value);let pageWidth, pageHeight;if (sizeSetting === ‘fit’) {
// If fitting, use image dimensions (scaled back to 1x logical size)
// PDF unit is 72 pts per inch.
// Just use pixel dimensions as points directly for “fit”
pageWidth = img.width;
pageHeight = img.height;
} else {
const standard = sizeSetting === ‘a4’ ? PageSizes.A4 : PageSizes.Letter;
let isLandscape = false;
if (orientation === ‘landscape’) isLandscape = true;
else if (orientation === ‘auto’) isLandscape = img.width > img.height;pageWidth = isLandscape ? standard[1] : standard[0];
pageHeight = isLandscape ? standard[0] : standard[1];
}const page = pdfDoc.addPage([pageWidth, pageHeight]);// 4. Draw Image on Page
const availW = pageWidth – (margin * 2);
const availH = pageHeight – (margin * 2);// Calculate aspect ratio fit
const scale = Math.min(availW / img.width, availH / img.height, 1);
// Note: if ‘fit’ mode, scale usually ends up 1.0 because avail matches img// Centering
const drawW = img.width * scale;
const drawH = img.height * scale;
const x = (pageWidth – drawW) / 2;
const y = (pageHeight – drawH) / 2;page.drawImage(pngImage, {
x: x,
y: y,
width: drawW,
height: drawH,
});const pdfBytes = await pdfDoc.save();
const blob = new Blob([pdfBytes], { type: ‘application/pdf’ });
const url = URL.createObjectURL(blob);
const a = document.createElement(‘a’);
a.href = url;
a.download = currentFile.name.replace(‘.svg’, ‘.pdf’);
a.click();} catch (err) {
console.error(err);
alert(“Error generating PDF.”);
} finally {
btnConvert.disabled = false;
btnText.innerText = ‘Download PDF’;
btnLoader.classList.add(‘hidden’);
}
}function formatBytes(bytes) {
if (bytes === 0) return ‘0 Bytes’;
const k = 1024;
const sizes = [‘Bytes’, ‘KB’, ‘MB’];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ‘ ‘ + sizes[i];
}function resetApp() {
fileInput.value = ”;
uploadedSVGUrl = null;
dropzone.classList.remove(‘hidden’);
resultArea.classList.add(‘hidden’);
}function toggleDarkMode() {
isDarkMode = !isDarkMode;
document.documentElement.classList.toggle(‘dark’);
const icon = document.getElementById(‘theme-icon’);
if(isDarkMode) {
icon.classList.remove(‘fa-moon’);
icon.classList.add(‘fa-sun’);
} else {
icon.classList.remove(‘fa-sun’);
icon.classList.add(‘fa-moon’);
}
}// — JAVASCRIPT SEPARATION END —
About the SVG to PDF tool The SVG to PDF page is part of Articles Stock’s online utilities collection. Use the tool above for the task described in its interface, and check the available file types, settings and output before proceeding. This utility relates to document conversion and PDF workflows.
How to use this tool Review the tool’s supported input and options. Select or enter the required input and choose the settings that suit your task. Run the tool and check the result before saving or sharing it. Related online tools Related PDF tools: PDF to SVG and Compress PDF . Review the converted file before sharing.