Convert SVG to PDF in C#
IronPDF converts SVG graphics to PDF documents using the HTML to PDF approach - embed your SVG in an img tag with explicit width/height styles to ensure proper rendering.
IronPDF supports rendering SVG graphics into PDF documents via the HTML to PDF method. SVG (Scalable Vector Graphics) files are widely used for logos, icons, illustrations, and charts due to their scalability and crisp rendering at any size. Converting SVGs to PDFs is essential for creating print-ready documents, archival purposes, and ensuring consistent display across different platforms.
Note: Set the width and/or height style attribute of the img element when embedding an SVG - otherwise, it may collapse to zero size and not appear in the rendered PDF.
Quickstart: Effortless SVG to PDF Conversion
Convert SVG files to PDF using IronPDF in C#. This snippet demonstrates embedding an SVG via an HTML img tag with specified dimensions, a crucial step for successful rendering. Follow this guide for a quick implementation that ensures your SVGs are accurately rendered and saved as PDFs.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
new IronPdf.ChromePdfRenderer { RenderingOptions = { WaitFor = IronPdf.Rendering.WaitFor.RenderDelay(1000) } } .RenderHtmlAsPdf("<img src='https://example.com/logo.svg' style='width:100px;height:100px;'>") .SaveAs("svgToPdf.pdf");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Install IronPDF Library for SVG to PDF Conversion
- Use
imgtag in HTML to import SVG image - Utilize different rendering methods in IronPDF to generate PDF
- Save the PDF file containing SVG image with
SaveAsmethod - Check the PDF in specified location
How Do I Render SVG to PDF with Proper Sizing?
Many browsers tolerate SVGs without size specifications; however, our rendering engine requires them. The Chrome rendering engine used by IronPDF requires explicit dimensions to properly render SVG elements. Without specified dimensions, the SVG may not appear in the final PDF or may render with unexpected sizing.
When working with SVGs in IronPDF, you have several options for ensuring proper rendering:
- Inline SVG with Style Attributes: Add width and height directly in the style attribute
- External SVG Files: Reference SVG files via URL or local file path
- Base64 Encoded SVGs: Embed SVGs directly in the HTML as Base64 strings
For more advanced HTML rendering options, see our comprehensive guide on rendering options.
:path=/static-assets/pdf/content-code-examples/how-to/SVGs-render.csusing IronPdf;
string html = "<img src='https://ironsoftware.com/img/svgs/new-banner-svg.svg' style='width:100px'>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.RenderDelay(1000);
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("svgToPdf.pdf");What Does the Generated PDF Look Like?
Additionally, or alternatively, an SVG node may have an explicit width and height attribute assigned. See also examples of SVG styling on CodePen.
Render SVG to PDF Example
Working with Local SVG Files
When converting local SVG files to PDF, use the file path approach. This method works well with SVG assets stored in your project:
using IronPdf;
using System.IO;
// Load SVG file content
string svgPath = @"C:\assets\company-logo.svg";
string svgContent = File.ReadAllText(svgPath);
// Create HTML with embedded SVG
string html = $@"
<html>
<head>
<style>
body {{ margin: 20px; }}
.logo {{ width: 200px; height: 100px; }}
</style>
</head>
<body>
<h1>Company Report</h1>
<img src='file:///{svgPath}' class='logo' />
<p>Annual financial summary with vector graphics.</p>
</body>
</html>";
// Configure renderer with custom settings
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 10;
renderer.RenderingOptions.MarginBottom = 10;
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
// Generate PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("report-with-svg.pdf");using IronPdf;
using System.IO;
// Load SVG file content
string svgPath = @"C:\assets\company-logo.svg";
string svgContent = File.ReadAllText(svgPath);
// Create HTML with embedded SVG
string html = $@"
<html>
<head>
<style>
body {{ margin: 20px; }}
.logo {{ width: 200px; height: 100px; }}
</style>
</head>
<body>
<h1>Company Report</h1>
<img src='file:///{svgPath}' class='logo' />
<p>Annual financial summary with vector graphics.</p>
</body>
</html>";
// Configure renderer with custom settings
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 10;
renderer.RenderingOptions.MarginBottom = 10;
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
// Generate PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("report-with-svg.pdf");Base64 Encoding for Embedded SVGs
For scenarios requiring SVG data embedded directly in HTML without external file references, Base64 encoding provides a reliable solution. This approach is explained in detail in our embedding images guide:
using IronPdf;
using System;
// SVG content as string
string svgContent = @"<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100'>
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='2' fill='red' />
</svg>";
// Convert to Base64
byte[] svgBytes = System.Text.Encoding.UTF8.GetBytes(svgContent);
string base64Svg = Convert.ToBase64String(svgBytes);
// Create HTML with Base64 embedded SVG
string html = $@"
<html>
<body>
<h2>Embedded SVG Example</h2>
<img src='data:image/svg+xml;base64,{base64Svg}' style='width:150px;height:150px;' />
</body>
</html>";
// Render to PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("embedded-svg.pdf");using IronPdf;
using System;
// SVG content as string
string svgContent = @"<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100'>
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='2' fill='red' />
</svg>";
// Convert to Base64
byte[] svgBytes = System.Text.Encoding.UTF8.GetBytes(svgContent);
string base64Svg = Convert.ToBase64String(svgBytes);
// Create HTML with Base64 embedded SVG
string html = $@"
<html>
<body>
<h2>Embedded SVG Example</h2>
<img src='data:image/svg+xml;base64,{base64Svg}' style='width:150px;height:150px;' />
</body>
</html>";
// Render to PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("embedded-svg.pdf");Best Practices for SVG to PDF Conversion
1. Always Specify Dimensions
The most common issue when converting SVGs to PDF is missing or zero dimensions. Always ensure your SVG elements have explicit width and height values. For responsive designs, consider using viewport settings to control PDF layout.
2. Handle Complex SVGs with Render Delays
Complex SVG files with animations or JavaScript may require additional rendering time. Use the RenderDelay option to ensure complete rendering:
renderer.RenderingOptions.WaitFor.RenderDelay(2000); // Wait 2 secondsrenderer.RenderingOptions.WaitFor.RenderDelay(2000); // Wait 2 secondsFor advanced JavaScript handling, explore our JavaScript rendering guide.
3. Optimize SVG Files
Before conversion, optimize your SVG files by:
- Removing unnecessary metadata
- Simplifying paths
- Converting text to paths for consistent rendering
- Using appropriate compression
4. Test Cross-Platform Compatibility
SVG rendering may vary across different operating systems. Test your conversions on:
Troubleshooting Common SVG Issues
SVG Not Appearing in PDF
If your SVG doesn't appear in the generated PDF:
- Verify dimensions are set correctly
- Check file paths or URLs are accessible
- Ensure proper MIME type for SVG files
- Review our pixel-perfect rendering guide
Scaling and Resolution Problems
For high-quality SVG rendering at different scales:
// Set custom DPI for better quality
renderer.RenderingOptions.DPI = 300;
// Use CSS transforms for scaling
string html = @"
<img src='logo.svg' style='width:200px;height:200px;transform:scale(1.5);' />";// Set custom DPI for better quality
renderer.RenderingOptions.DPI = 300;
// Use CSS transforms for scaling
string html = @"
<img src='logo.svg' style='width:200px;height:200px;transform:scale(1.5);' />";Font Rendering in SVGs
When SVGs contain text elements, ensure fonts are properly embedded. Learn more about font management and web font support.
Advanced SVG Conversion Techniques
Batch Processing Multiple SVGs
For converting multiple SVG files to a single PDF document:
using IronPdf;
using System.Collections.Generic;
using System.Text;
List<string> svgFiles = new List<string>
{
"chart1.svg",
"chart2.svg",
"diagram.svg"
};
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.Append("<html><body>");
foreach(string svgFile in svgFiles)
{
htmlBuilder.Append($@"
<div style='page-break-after:always;'>
<img src='{svgFile}' style='width:600px;height:400px;' />
</div>");
}
htmlBuilder.Append("</body></html>");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlBuilder.ToString());
pdf.SaveAs("multiple-svgs.pdf");using IronPdf;
using System.Collections.Generic;
using System.Text;
List<string> svgFiles = new List<string>
{
"chart1.svg",
"chart2.svg",
"diagram.svg"
};
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.Append("<html><body>");
foreach(string svgFile in svgFiles)
{
htmlBuilder.Append($@"
<div style='page-break-after:always;'>
<img src='{svgFile}' style='width:600px;height:400px;' />
</div>");
}
htmlBuilder.Append("</body></html>");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlBuilder.ToString());
pdf.SaveAs("multiple-svgs.pdf");For more information on working with multiple pages, see our merge or split PDFs guide.
Ready to see what else you can do? Check out our tutorial page here: Additional Features. You can also explore how to add headers and footers to your SVG-containing PDFs or learn about custom watermarks for branding your documents.
- Use
TextAnnotationandAddTextAnnotation
Frequently Asked Questions
How do I convert SVG to PDF in C#?
IronPDF converts SVG to PDF using the HTML to PDF method. Simply embed your SVG in an HTML img tag with explicit width and height styles, then use IronPDF's ChromePdfRenderer to render the HTML as a PDF. The key is ensuring proper sizing attributes are set on the img element.
Why isn't my SVG appearing in the PDF?
SVGs may not appear in PDFs generated by IronPDF if they lack explicit width and height specifications. The Chrome rendering engine used by IronPDF requires dimensions to be set either through style attributes or width/height attributes on the img tag. Without these, the SVG may collapse to zero size.
What are the different methods to embed SVGs for PDF conversion?
IronPDF supports three main methods for embedding SVGs: 1) Inline SVG with style attributes for width and height, 2) External SVG files referenced via URL or local file path, and 3) Base64 encoded SVGs embedded directly in the HTML. All methods require proper sizing specifications.
Can I convert local SVG files to PDF?
Yes, IronPDF can convert local SVG files to PDF. Use the file path approach by referencing your SVG assets stored in your project through the img tag's src attribute. Remember to include width and height specifications for proper rendering.
What rendering options should I use for SVG to PDF conversion?
When converting SVG to PDF with IronPDF, use the ChromePdfRenderer with appropriate RenderingOptions. A common approach is to add a RenderDelay using WaitFor.RenderDelay(1000) to ensure SVGs load completely before conversion. This helps with complex SVGs or when loading external resources.
How do I save the converted SVG as a PDF file?
After rendering your SVG-containing HTML with IronPDF's ChromePdfRenderer, use the SaveAs method to save the PDF file. Simply call SaveAs("filename.pdf") on the rendered PDF object to save it to your specified location.






