C# create a pdf from html that uses react - c#

I'm looking for a way to have server side rendered pdf from a html+css+javascript source. The idea here is to have a preview and report designer available on the client side using react components.
I've tried DinkToPdf that can render PDF from html pages, however it seems like only the top level component was mounted in the page when the PDF was rendered, even when enabling javascript in the renderer.
Client side pdf rendering using canvases is not an option since data can be easily tampered with.

I did this recently using wkhtmltopdf, which is a command line version of webkit (which I think Chrome is based on). I was able to load/parse and generate a graph using css/d3/rickshaw.
The basic process is:
Create the HTML using RazorLight
shell out to wkhtmltopdf with the generated html.
do something with generated pdf.

Related

HiQPdf not displaying Charts on the generated PDF

I have been using the (free version) HiQPdf libraries to converto html pages to pdf documents.
I am also using in my pages several Chart objecs from the .Net Framework (System.Web.UI.DataVisualization.Charting.Chart) to produce bar graphics dysplaying values changing along time. It works wonders in my local environment when I debug with VS, but when I publish on my IIS or on other servers the charts do not appear at all on the pdf - note: they do appear on the webpages just not on the pdf.
In the Pdfs all the html is displayed correctly, inlcuding css, showing it exactly as seen on the page, except the chart images. I kind of understand that they would not appear as in the html, the image source from the charts results in something like:
<img id="MainContent_MyPageControl_ctl00" src="/MyTestWebSite/ChartImg.axd?i=charts_0/chart_0_2.png&g=396d61e14ceb41c08be06fd956cd4dca"
Because the real generated png image is not even directly referenced as usual. But the fact is they do appear on the generated pdf when running local from VS, which produces a similar html anyway as the above.
Only difference I see is that when I run local the Image Chart Handler key is defined in the webconfig as:
add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;"
but when publishing on a server I have changed it to:
add key="ChartImageHandler" value="storage=file;timeout=20;Url=~/MyfolderTempcharts/;deleteAfterServicing=false;"
So why dont the charts appear too on my generated pdf, or why they do appear if Im running from VS?
Anyone ever had this problem?
So I have figured out a way to deal with my problem, and I am sharing here if anyone is also in need. The deal is that the Chart objects use http handlers to render the chart generated images (you can read more about it in detail from these guys: https://web.archive.org/web/20201205231110/https://www.4guysfromrolla.com/articles/081909-1.aspx)
At first I thought I could manipulate the html (that I get before the page render) and replace the image sources with accessible links to send to the HiQPdf - for that I would need to temporarely copy the generated images to a public folder. That would be fine unless in my case I deal with sensitive data that should never be accessed even if only momentarily.
So what I do is I check that the image file does exists (in my configured folder) and extract the byte data from it, convert it to base 64 and replace it in the image source html where the handler is, and voila.
example of Base64 html embeded image:
Notice that this manipulated html is never accessible from the outside and is only worked on on the server side, in order to provide a "normal" html that the HiQPdf can pprocess.

Generating wide html pages to PDFs

I'm using C# .Net and want to generate a random size HTML table into PDF. I have tried ExpertPDF and Essential Objects HTMLtoPDF but they seem to mess up on very wide tables.
Any tools or ideas to fix this?
These tools use html rendering engines like a regular browser. Then they take the rendered content and write it into a pdf. They don't know how to handle wide content (except for scaling it to fit into a pdf page).
You need to change your html (create a printer friendly version of your page that is not that wide) and feed the html to pdf converter something that it can chew.

Whats more efficient: displaying dynamic HTML or rendering PDF and embedding it

I am building a large scale web application with many "print" requests a minute. We have dynamic content from an editor that I print out with other HTML elements. What is more efficient, rendering that html and then printing the html page or rendering a PDF server side and displaying that in an iframe to be printed?
With the multiple server side calls for rendering the images plus authenticating the user (and we also have a dynamic image header and footer dependent on the logged in user) would it be more efficient to render native PDF controls and then put a pdf in an iframe to print?
I was planning on using itext sharp or http://www.websupergoo.com/ for the rendering.
the app is a c# Web forms application.
As I see it in order to generate the PDF you need to generate the HTML. So by generating the PDF you are adding to the processing required not reducing it.

asp.net pdf renderer

I want to ask you how to render a pdf in asp.net using c#.
so my web can show a pdf.
You can use Aspose.PDF or iText (there is a free version).
You can read about the licensing for iText for more information on the comment provided below.
One way is to generate latex from your application and then convert latex into pdf:
http://www.emphess.net/2010/11/09/create-high-quality-pdfs-with-razor-view-engine-and-latex
The nice thing about doing it this way is that you can write your views just like all your other views, except you use latex instead of html.
You can use the ReportViewer component (in "Local Processing Mode", so you don't need the SQL Server Reporting Services) to show a Report created in Visual Studio in a web page.
If you want the report to output directly as a PDF (instead of being shown in the ReportViewer control), you can use the technique outlined in this article to render the report on the server and then send the PDF stream to the web client:
How to render client report definition files (.rdlc) directly to the Response stream without preview
The advantage of this solution is that you don't need to use any third-party libraries.

View PDF through C# .Net desktop App

I want to know how I can view a PDF through a C# .net desktop App. I am trying to create a application to view PDF using visual studio 2008
There is a pdf reader libraries called iText(iTextSharp). But it didn't help me
You can host a ie web browser control in your application and that will allow the user to view a pdf if they have a reader installed.
I can provide an example if you tell me whether you are using WPF or WinForms.
Drag WebControl on to you form
Set the path in code
Done Press F5
I'm not sure what netbeans has to do with anything, but take a look at this question here How to render pdfs using C#
Essentially you need to get a 3rd party PDF viewer or write one yourself. There are quite a few around and would probably take a look at something like PDFViewForNet
iText isn't a PDF viewer.
If you want to read PDF documents in your application there are couple of Open Source PDF Libraries.

Categories

Resources