Load picture from a .NET assembly resource in a simple HTML file? - c#

I have a .NET Server Control app that simply returns some HTML. I also need to embed several picture files into the assembly so that the HTML file can use them as its src= for each of them.
We will simply have a .HTML file that lives in the project as an embedded resource and the server control code will read this html and serve it up. Within THAT html, we will need to have all the picture src links (as well as CSS, js, etc) to point back to embedded resource files.
Does anyone know what code I would put in the HTML file for the pictures to make it point back to the embedded picture file?
I have to do this on a grand scale... hundreds of times. I really would like a programmatic approach to doing this so I can write a wrapper and never have to touch it again when we update the server control with new html, picture files, etc.
One might imagine a way to do this at compile time where I can loop through the embedded files with GetManifestResourceNames and then replace() the src links with the HTTP resource links I suppose?
Thank you for any guidance!

Hm, your question covers quite many aspects. Let me repeat to see if I got it: You have an assembly, with a raw HTML file in it. This file references some items, which are to be found within this same assembly, and you want to have them served to the client upon request as well.
One possible solution might be this.
Instead of a raw HTML file, use a templated one. Then, feed all available resource names as proper URL's into the templating engine, to replace the placeholders.You may want to look at DotLiquid for this.
Create a HTTP handler for each file type you want to serve. Inside the handler, you pull the item from the resources of the dll and serve them.
Alternatively, if those resources are rather small, you want to have a look into the data URI scheme, to save the extra requests and omit the handler. With this you could replace the placeholders with the data URI's directly, and serve a single HTML file with everything in it in the frist place.

Another choice is to have your .NET Server Control app check for optional GET arguments and return the image instead of the HTML.
Your original HTML request might be a simple:
GET netServerApp
Which returns the HTML with normal embedded links.
The HTML image links in the HTML might look like this:
<img src="netServerApp?src=Image1.svg">
or the like. Your server app would then return the appropriate image, instead of the HTML.
It means several round trips to get everything, but that is normal for HTML anyway.

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.

How to cache web to read offline in webcontrol window phone

I'm using to load website follow code
webContent.Navigate(new Uri(linkURL));
I want to cache all content and html tag, style, js in web to read offline.
I tried download html source, file css and js using Webclient and replace these file to html resource then save to file "index.htm" but not good.
Can you find the way to resolve this issue? thank you.
The only way to do this is to download ALL the relevant files (including those referenced inside the HTML. eg. images, css, js, etc.) and save them ALL to Isolated Storage with appropriate similar file and folder structures.
The important point is that you also need to update all the paths within the content so that they point to relative paths that match where files have been saved.
You can then load the HTML from IsolatedStorage.
This is potentially a lot of work. I'd recommend exploring other options if possible first. Also remember to manage the files stored in IsolatedStorage appropriately so you don't just keep adding files there indefinitely.

How to convert a HTML Table in an MVC View to PDF file, ITextSharp

I've been pointed in the direction of ITextSharp, when I went to download the package from NuGet I noticed something called RazorToPDF only to discover unsolvable formatting issues due to the project no longer being supported.
After more research I was surprised to find there wasn't a similarly worded question as this on SO.
So guys, what's the best way to convert a HTML page/table in an MVC project to a PDF file?
What's the best way to convert a HTML page/table in an MVC project to a PDF file?
Generally, print it to a PDF from the web browser on the client.
The thing is, by relying on the end-user perspective of the view in this case, you're also relying on the end-user rendering of that view. It's a step that should be removed from this particular equation entirely.
Keep in mind that there are fundamental differences between how an HTML page renders and how a PDF renders. The two aren't 100% interchangeable. A PDF has a static page size and elements are placed absolutely, whereas HTML has dynamic sizes and elements are placed in a flow layout. There are additional considerations such as client-side DOM manipulation that may take place in that view. "Rendering" it quickly becomes a browser-based activity, which is something you shouldn't really need to do server-side.
Instead of thinking of the PDF as an extra step following the rendering of the view, think of it as a view in and or itself, parallel to the other view. One requested action results in the HTML view, another requested action results in the PDF "view". As such, you design the PDF template how you want it to look and populate it with data (using something like iTextSharp) before returning the file contents to the client.

Sending html file with images via email

How can i send html mail with images in c#? Can i just set direct link to image on my server(something like <img src="http://mysite.ru/img.png" /> ) or i need attache image and then use link to my attached file? Is there any examples or ready to use libraries? What about css files and scripts?
There are multiple methods of coding in-line images. We use VB.NET in house and this site is an excellent reference http://www.systemnetmail.com/default.aspx
If you directly embed the links into the email (pointing to an external server) you'll get blocked by most clients, but the user can turn them on.
They can be embedded in the usual manner of:
<img src="http://go.com/go.gif" />
If you need it to show up by default you'll need to embed the image as an attachment and link to that attachement inline.
See: http://www.systemnetmail.com/faq/2.6.aspx
For CSS it is common to use inline style and no referenced files the images on the other side should be just links and not send with the email. You just need to declare the mail content as html and you are ready to go. JavaScript is also available I use it fir some repositioning.
See http://en.wikipedia.org/wiki/MHTML:
MHTML, short for MIME HTML, is a web
page archive format used to combine
resources that are typically
represented by external links (such as
images, Flash animations, Java
applets, audio files) together with
HTML code into a single file. The
content of an MHTML file is encoded as
if it were an HTML e-mail message,
using the MIME type multipart/related.
The first part of the file is normally
encoded HTML; subsequent parts are
additional resources identified by
their original URLs and encoded in
base64. This format is sometimes
referred to as MHT, after the suffix
.mht given to such files by default
when created by Microsoft Word,
Internet Explorer, or Opera. MHTML is
a proposed standard, circulated in a
revised edition in 1999 as RFC 2557.
Learning more about MHTML may be the key to solving the problem.
probably the best option is to use some package like MvcMailer.
Mvc Mailer display the following characteristics:
MvcMailer sends emails using the MVC views as Email Body with no effort. Here's a quick list of features:
a) Use Razor/WebForms views
b) Use Master pages
c) Write Testable Code
d) Pass values to your view using ViewBag or ViewModel and
e) Generate Absolute URL using Url.Abs method and
f) create multi-part emails by just adding a view file,
g) scaffold your mailers. MvcMailer has a MailerBase class that extends ControllerBase class.
So, Mailers are just like your Controllers. As a result, you can use all the controller goodies without any learning curve, period. The end result is a professional looking HTML email body ready for your to send to your website users.
Visit the Project site for a comprehensive tutorial on MvcMailer. https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
On the step guid you will find how you can send emails using images.
brgds.
sebastian.

Scraping content from webpage

I need to scrape a remote html page looking for images and links. I need to find an image that is "most likely" the product image on the page and links that are "near" that image. I currently do this with a javascript bookmarklet so that I am able to get the rendered x/y coordinates of images and links to help me determine if those are the ones that I want.
What I want is the ability to get this information by just using a url and not the bookmarklet. The issues it that by using the url and trying something like httpwebrequest and getting the html on the server, I will not have location values since it wasn't rendered in a browser. I need the location of images and links to help me determine the images and links that I want.
So how can I get html from a remote site on the server AND use the rendered location values of the dom elements to help me locate images and links?
As you indicate, doing this purely through inspection of the html is a royal pain (especially when CSS gets involved). You could try using the WebBrowser control (which hosts IE), but I wonder if looking for an appropriate, supported API might be better (and less likely to get you blocked). If there isn't an API or similar, you probably shouldn't be doing this. So don't.
You can dowload the page with HttpWebRequet and then use the HtmlAgilityPack to parse out the data that you need.
You can download it from http://htmlagilitypack.codeplex.com/

Categories

Resources