Ways to Convert RAZOR (.razor File) to PDF
The functionality of Razor (.razor) files is significant in Blazor applications as it features dynamic web pages using C# and HTML. Nonetheless, for one reason or another, there comes a time when a user wants to convert Razor to PDF document. Maybe its for documentation, archiving, or just sharing some static representation of your dynamic content.
There is no direct “save as PDF” option for .razor files, but a few practical methods can accomplish the task. In this article, we will look at different ways of converting .razor files to PDF by both manual and automated means.
Reasons for Conversion of .RAZOR Files to PDF
Before moving into the procedures, let’s look into why converting a .razor file to the PDF might be useful. Here are some of the reasons:
- Documentation: Saving code prototypes for references or for documentation purposes.
- Reporting: Converting rendered Razor pages to a PDF for business reports.
- Archiving: Saving a static version of your Web pages.
- Sharing: Share formatted content that does not require a Blazor environment.
Now let us talk about the best options to efficiently convert Razor to PDF.
Manually Export Rendered Razor Page as PDF
The simplest method to do this manualy is to load the Razor page in a browser, and use the built-in print-to-PDF feature. This feature is present in almost all modern browsers, such as Chrome, Firefox, Safari, Opera, Brave, etc.
These are the steps:
- Run the Blazor application, then navigate to the Razor page that is intended for conversion.
- Use Ctrl + P (or Cmd + P for macOS) to initiate the print dialog.
- Select Save as PDF as the printer destination.
- Click Save, select the desired location and download the PDF.
✔ Pros: Quick and does not require any extra software.
✖ Cons: Formatting may not be perfectly maintained, and interactive elements will not make the cut; unsuitable for a batch conversion.
Use C# with a PDF Library (IronPDF, iTextSharp, or Syncfusion)
If an programming approach suits you, then the PDF documents can be generated directly from within a Blazor application using tools such as iTextSharp, IronPDF, or the Syncfusion PDF Library. This way, the method extracts Razor-generated HTML and converts it to a structured PDF for a high degree of customization: styling, headers, footers, and watermarks.
This is an example using iTextSharp:
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public void ConvertRazorToPdf(string htmlContent, string filePath)
{
Document pdfDoc = new Document();
PdfWriter.GetInstance(pdfDoc, new FileStream(filePath, FileMode.Create));
pdfDoc.Open();
pdfDoc.Add(new Paragraph(htmlContent));
pdfDoc.Close();
}
This is an example using IronPDF:
using IronPdf;
var Renderer = new ChromePdfRenderer();
var pdf = Renderer.RenderHtmlAsPdf("<h1>Hello, Razor to PDF!</h1>");
pdf.SaveAs("Output.pdf");
✔ Pros: Full control, automation-friendly, customizable output.
✖ Cons: Needs coding, make sure to review licensing costs for commercial use.
Convert .RAZOR Files Using SysTools (For Text-based Conversion)
In case you need just the raw Razor code conversion rather than a full-blown conversion of a rendered page, you can rely on SysTools Universal Document Converter. The tool works brilliantly for text-based conversion. Understand that it does not render the actual output of a web page but only helps you convert the raw code.
If you often need to convert multiple .razor files to PDF, this professional converter will save you a great deal of time and effort. Here are some key features of the tool:
- Batch .razor to .pdf conversion
- Retains formatting and code structure
- User-friendly for a non-coder
- Fast processing for bulk files
- Multiple options to customize the output PDF
- Page layout
- Header & footer
- Font
- Bates number
- Add password to lock PDF file
Stepwise Guide to Convert Razor Raw Code to PDF
- Firstly, use either Add Folder or Add File button to add .razor files into the software.
- In the next window, preview the added files by clicking on them.
- After that, modify the PDF setting (PDF is the sole export format so no need to select it separately).
- Click on Change to change the destination location of the saved files.
- Finally, click on the Export button to start converting all added .razor files to PDF documents.
- Once the process is successfully completed, open the destination folder to verify the PDF files.
✔ Pros: Perfect for bulk conversions, easy to use, keeps formatting of raw code.
✖ Cons: Paid software, worth the penny for heavy-duty conversion. Does not render web page output.
End of Article
It is quite elementary to convert .razor files to PDF for documentation and reporting. Depending on your requirements, there are different ways to carry out this conversion.
- For one-page export on the fly → Use the Print-to-PDF feature from your browser.
- For automated solutions in Blazor → Use a C# PDF Library, for instance, IronPDF.
- For raw conversion of Razor code & batch processing → Use SysTools Software.
There are pros and cons to each solution, so you need to choose the one that best suits your interest. Test any of these methods on some sample files and find your way of converting Razor to PDF. That’s all in this article. Read our other blogs for more technical know-how.