C# Winnovative HTML to PDF - c#

I am searching for a solution to convert HTML to PDF with external CSS support. I downloaded the trial version of the Winnovative Toolkit Total v11.14, and tried out the demo application for the method public byte[] GetPdfBytesFromHtmlString (string htmlString, string urlBase). The PDF files are generated, but the CSS is not applied.
Note: I tried the same input HTML string and base URL in the demo site. It's working fine, so I don't know why it's not working in my system. The demo application is shared in v11.14 ZIP files.
Input provided for this method:
htmlString = HTML source of the url 'http://www.winnovative-software.com/'
urlBase = "http://www.winnovative-software.com/"

Are you using any proxy to access Internet? In this case you should set the HtmlToPdfConverter.ProxyOptions object properties in your code.

Related

SelectPDF ConvertUrl Non-English Characters Error

In my demo project I'm using Selectpdf tool to convert html pages to pdf documents. These html pages are stored locally. So I'm using ConvertUrl function for conversion. Here is the inline code
`
string url = AppDomain.CurrentDomain.BaseDirectory + "HTML" + "\\OrderName_" + DateTime.Now.ToString("yyyy'-'MM'-'dd'_'HH'-'mm_") + MockOrderNo + ".html";
HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertUrl(htmlurl);
`
Then I save the pdf document, using doc.Save(). Here is the pdf document result
Now as you can see there is a problem displaying Turkish characters like "İ,ı,ş,ğ...". How can I resolve this using SelectPdf? If solving this with SelectPdf is not possible, what are the other prefable pdf conversion tools that does not have this kind of problem?
Also for my requirements I don't use ConvertHtmlString function. I need to store html pages in a folder, convert these html pages to pdf and store those pdf documents in an another folder.
Thanks for your help
I just changed the encoding of html file to windows-1252. This solved the problem

C# MVC generate PDF from View with NReco.PdfGenerator

I'm currently working on a project where I need to create a "dashboard" which can be exported as pdf. I wanted to use Rotativa but as our application uses .NET framework 4.0 it's not possible. So I found the NReco PdfGenerator.
Now that's the code how I create the PDF result:
var ViewAsString = RenderViewAsString("~/Views/QMetrics/StandardDashboard.cshtml", viewModel);
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.PageWidth = 1600;
htmlToPdf.PageHeight = 900;
var pdfBytes = htmlToPdf.GeneratePdf(ViewAsString);
FileResult FileResult = new FileContentResult(pdfBytes, "application/pdf");
FileResult.FileDownloadName = "Dashboard-" + viewModel.ProjectName + "-" +
DateTime.Now.ToString() + "-.pdf";
return FileResult;
It successfully creates the PDF page with all the content that comes from the backend (Project information, and so on) but the page looks very ugly. On the original page I have 2 columns and on the PDF page it puts everything in one column. I tried a few different page sizes and I also changed the layout to be non-responsive but nothing has changed.
My first suggesstion was that the referenced CSS and JS files are not included when the PDF get's created, so I copied all the stuff that comes from external files (bootstrap, Chart.js) and pasted it directly in the .cshtml file. But nothing changed at all. My Chart is not rendering/loading and the missing CSS stuff is still not there.
On the NReco PDFGenerator website they say that it supports complex CSS code and also javascript code so I don't really understand why this is not working.
Has anyone here experiences with NReco or can someone recommend something else that works for .NET 4.0?
NReco PdfGenerator internally uses wkhtmltopdf tool, so you can check it and its options.
Regarding 2 columns: if you don't use flex/grid layout everything should work fine. Possibly you need to disable wkhtmltopdf smart shrinking logic (enabled by default) and define web page 'window' size explicitely (with "--viewport-size 1600" option).
Regarding CSS and charts: you need to check that CSS files could be accessed by wkhtmltopdf, simplest way to do that is running wkhtmltopdf.exe from the command line and check console log output (or, handle PdfGenerator's "LogReceived" event in C#). For Chart.js ensure that chart container div has explicit width (not in %), and that there are no js errors (you can get them in console by specifying "--debug-javascript" option). If your js code uses 'bind' method you have to include polyfill as WebKit engine version used in wkhtmltopdf doesn't support 'bind'.

Convert SMTP Client Mail message to HTML

I have an issue with an mvc4 application in witch i would like to send a generated html page created with HTML + C#. The problem is that when i recieved the email i see my c# code like the exemple bellow:
Recieved Email
But in the mail preview i can see the correct values like this:
Mail Preview
So this is my EmailTemplate method:
<pre>
public static async Task<string> EMailTemplate (string template)
{
var templateFilePath =HostingEnvironment.MapPath("~/Views/Home/") + template + ".cshtml";
StreamReader objstreamreaderfile = new StreamReader(templateFilePath);
var body = await objstreamreaderfile.ReadToEndAsync();
objstreamreaderfile.Close();
return body;
}
</pre>
Please if you have any idea how to convert my template to Html without inclouding my C# code.
Thanks,
Use Pure Html template (Not a cshtml view) with Inline CSS and Absolute image URIs for email body and fill it text templates. See the below image.
In the above image, field with ##...## are text templates.
Now read this HTML template as a string and replace ##...## fields by some dynamic information using the C# code (Actually any language code). You can use string.Replace() method to replace these fields by actual values. I have already used this method and it is working fine. I hope this will help you too.

Display dynamic images in a flash file

I bought a website template that has a scrolling photo gallery. As it came, the images are static in the fla file itself. I would like to edit the fla and load images dynamically. Ideally from MSSQL. I'm using VS2010, C# webforms, and SQL Server 2008 R2.
Are there any code snippets or tutorials or general guidance on how to do this? I do have a CS3 disc with Flash on it I can use for editing.
You can use a Loader + URLRequest, something like: (untested code)
var imgLoader:Loader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageHasBeenLoaded);
imgLoader.load(new URLRequest("imagePath/from/database.jpg"));
public function imageHasBeenLoaded(e:Event) {
//Get the loaded bitmap image, do what you want with it from here.
var img:Bitmap = Bitmap(e.target.content);
}
Of course you would also want to feed the file paths to flash, either by FlashVars or by hitting a web service type of page (or xml file) via a Flash URLLoader + URLRequest. I prefer an xml file myself.

Read a web page with all images in Base64-Embedded format

In my scenario I want to download the HTML of a page (any page on the Internet) programaticaly but also I want all of the images in the HTML to be in base64 embedded format (not referenced)
In other words, instead of :
<img src='/images/delete.gif' />
I want the downloaded html to look like this:
<img src="data:image/gif;base64,R0lGODl..." />
This way I don't need to go through the process of storing all images in directories, etc, etc.
Does any of you have any idea how this can be done? Or any plugin to do this efficiently?
Well, you'd need to:
Download the original HTML
Find each img element in the HTML (for instance using the HTML agility pack) and for each one:
If it's already using a data URL, ignore it
Otherwise:
Download the image
Encoded it in Base64 using Convert.ToBase64String
Replace the original img tag with one using the base64 version (either in the original string, or via a DOM representation)
Save the final HTML to disk
Is any of these steps causing you a particular problem? You could potentially make it quicker by downloading the images in parallel, but I'd get a serial version working first.
Instead of using a html page with images as base64 encoded strings in the src attribute you might consider using the MHTML format instead. Most browsers supports the format and it embeds all external resources (including images).
var msg = new CDO.MessageClass();
msg.MimeFormatted = true;
msg.CreateMHTMLBody("http://www.google.com", CDO.CdoMHTMLFlags.cdoSuppressNone, "", "");
var stream = msg.GetStream();
var mhtml = stream.ReadText(stream.Size);
Use a regular expression (regex) to extract URLs from img tags, translate them to absolute URLs using the Uri class, then use WebClient to download the target images. After that it's just a case of using Convert.ToBase64String to produce the Base64.

Categories

Resources