Save Rendered HTML to Image in Golang with Templating - html

I need to be able to build an html page and then save the rendered html output to an image. Before I switched to Go, I was accomplishing this with NodeJS and PhantomJS by building a "headless" webpage and capturing the image with a screenshot into an image file. I need to be able to do the same thing in Go. I know how to build the HTML template using the html/template package. But, I am lost on how I get essentially capture a screenshot of the rendered HTML and save it to an image with a specified width and height. This was fairly easy in PhantomJS by just calling the 'Render' method and passing the location of the image to save to. But, I can't find anyway to do this in Go.
Any help is greatly appreciated!

Related

How can I send an image from and HTML form to be analyzed by a python AI?

To dive a bit more in depth, for a Hackathon, a friend and I need to get an image from an HTML form and analyze it using an AI algorithm. We easily handled the HTML form part, but that hard part is sending the image to be analyzed by the algorithm.
How would we do this. Would we have to set the algorithm up on some sort of server and then post the image to it or would we have to somehow integrate the algo into the webpage so it can run there.
Also what frameworks would we need to use and is there a guide to this somewhere?
Thanks,
CantTouchThis
I can't provide you the code because it is a competition, but I can help you a bit:
First (if you are using python) make a script that downloads the photo. You have to download the page and parse html document, find your image with regex and download it.. You can use urllib2 for downloading and Beautiful Soup for parsing html file, or, use htmllib to extract all img tags (override do_img), then use urllib2 to download all the images. Make sure everything is inside a definition or method so that we can call it later in our main script.
Make sure that the images are saved in your same directory
Make the last and main script, first import the first script we have made, and the downloaded file(make sure to give the imports inside a try and except because we haven't downloaded the image yet, you will get a error), call the method or definition of the photo downloading script, then write the rest AI algorithm, tell the file name, after the algorithm executes and gave result, make sure you write a code at the end that deletes the picture, because you might in future be asked to download more images, so you can create a list of websites and use it using a for loop to change the web address in the first script.
Best of Luck!

How interpret HTML in IOS app using swift

I'm trying to implement my first iphone app but I'm kind a stuck. I have API which return as one of the JSON's response items HTML content (tags like p, img, em, a etc).
What is the best way to interpret HTML content on IOS? I have found basically two possible solutions:
1) UIWebView
I have try this and all content is easily loaded. But am not sure if its good way? I guess that every time user open this view all images (<img src="">) are loaded from internet (I have no control about downloading and caching images?)? Am also not sure how to style elements in webView?
2) UILabel / UITextView
I have tried this also and i can easily remove all html tags but i have no idea how to style text originally wrapped in html tags? I guess that I would have parse complete html a render it manually?
I would be really glad for every advice because I'm not sure what is the right way to continue.
Thank you.
Look up String manipulation in Swift. It's a very basic way of managing web content but it will give you great control. However i really don't recommend using an API that return html response because "Parsing" it will be very resource(CPU,RAM) consuming. Try an API with JSON response. Also if you want to cache. look up kingfisher framework.it's really efficient!
After some research I think that the right way is to use UIWebView. Because UILabel can interpret only basic HTML markup and I get more complicated HTML code like youtube embeds etc.
You can load easily HTML string using UIWebView's method loadHTMLString.
HTML content can be styled using local css. You can get path to local css file using:
NSBundle.mainBundle().URLForResource("myCssFile", withExtension: "css")
If you have local path to css you can add it to html header as usual.
If anybody know better way how to do this i still would be glad for any ideas.

render a full web page in node.js code

I am running a node.js server, and it is rendering a web page wonderfully. When I look at this in a browser, it runs exactly as I expect.
However, what I actually want to do is make the call to fully generate the html page - exactly as it is in the browser - within the node.js code, as a call. Currently, I have tried this:
http.request("http://localhost:8000/").end();
(with a few variants). This does exactly what it says, which is to make the single call to the server for the page - what it doesn't do is actually render the page, pulling in all of the other script files, and running the code on the page.
I have tried exploring express and ejs, and I think I need to use one of these, but I cannot find out how to do this fairly straightforward task. All it needs is to render an html page, but it seems to be a whole lot more complex than it should be.
What output do you want? A string of HTML? Maybe you want PhantomJS the headless browser. You could use it to render the page, then get the rendered DOM as a string of HTML.
Use the Mikeal's Request module to make http requests once you captured the response you then can inspect the html however you like.
To make that easier though you should use cheerio, this will give you a jQuery style api to manipulate the html.
Perhaps you are looking for wkhtmltopdf?
In a nutshell, it will render an entire web page (including images and JavaScript) to a PDF document.

How can I display local HTML in the windows phone web browser?

I want to use a web browser to present some multimedia information with locally stored HTML files. The problem is that it just presents the static content without any HTML elements, and also doesn't show the images. The sample code is:
StreamReader reader = new StreamReader(TitleContainer.OpenStream("pathString"));
webBrowser.NavigateToString(reader.ReadToEnd());
The best way to do this is to copy your html file and images to the same directory in Isolated Storage (see: http://msdn.microsoft.com/en-us/library/ff431811(v=vs.92).aspx). Then, you call
webBrowser1.Navigate(new Uri("<Location_of_html_file_in_isolatedStorage>", UriKind.Relative)).
If the images and the html file are in the same directory in isolatedStorage, relative links to your images in this format:
<img src="../YourImage.jpg"/>
Will work correctly.
I don't suppose the WebBrowser control could get access to any images that might be stored in isolated storage, so all the content you want displayed - you would probably need to somehow embed it in a single html, which would probably require some js code (I am not a js/html expert), which needs to be enabled in the control (IsScriptEnabled if I remember correctly). Otherwise - it might be better to convert your HTML to XAML or just otherwise parse it and display it by code.

PdfSharp, GDI+ and HTML printing

I currently have a "PrintingWebService" that I call from an AJAX page with all the information that is needed to construct a highly customized PDF printout using PDF Sharp and the PDFSharp's GDI+ mode, which takes DrawString and other commands that work basically just like GDI+ only they are drawn to the PDF.
I then save the PDF file to a location on the webserver and return the file name from the web service, and the AJAX page opens a new window with the pdf file.
So far, it works well, however, there is one part of my AJAX page that I want to printout and I haven't come up with a solution for yet. I've got a string of the HTML content of a TinyMCE editor that I want to dispay in the bottom part of the PDF page.
I'm looking for some sort of tool I could use for this purpose. Even something opensource that prints to GDI+ I could use by taking the source code and translating it to use PdfSharp's GDI+ (the class names are like XGraphics, with each class having X before the GDI+ name).
If I have to I will limit what HTML can be generated by TinyMCE and write my own renderer, but that will be a big challenge, so I'm looking for other solutions first.
I've stayed away from a printer-friendly page approach because I wanted to construct a page that was a near identical of an existing WinForms printout, using my existing code. With PdfSharp I was able to convert all the code except the text area stuff (which used the RichTextBox and RTF in the WinForms version).
Tony,
I personally have used WebSupergoo's ABCPdf library with much success. You can actually render HTML directly to the PDF and it does fairly well in regards to accuracy.
Another free software that will allow you the flexibility of writing HTML to PDF that I have used in the past with much success is iTextSharp.
Otherwise, I think you'll have to write something to render HTML to GDI.
Either way, you may want to consider using an HttpHandler that you map to using your web.config to generate the PDF file. This will allow for you to render the PDF to a bytestream and then dump it directly to the user (as opposed to having to save each PDF receipt to the web server). It will also allow for you to use the .pdf extension in the page that returns the receipt (PurchaseReceipt.pdf could be mapped to a HttpHandler)... making it more cross-browser friendly. Older versions of Adobe / Browsers will not display correctly if you start throwing a PDF byte stream from an ASPX page.
Hope this helps.