Hi Im working on a MVC3 Razor project and I have been stuck on this problem for a few hours now...
I'm trying to get a html to pdf converter to serve a document to the user...
What I want is the following..
A page is rendered and displayed. On the bottom of the page there should be a little icon display something like download as pdf. and what that does is where my problem lies..
All the data that I want is dynamically created within a $("#content").html();
So what I have tried is a jquery/ajax function passing $("#content").html(); as a paramater to my function which creates the pdf (works but I have no clue how to prompt the created file to the user?)
Other solution was #(Html.ActionLink()) but I dont know how to pass the data ($("#content").html()) within that link?
And when trying to work with the functionallity to use the converter to go to the url was a dead end cuz it got its own session and got redirected to the loginpage..
Any help would be appriciated!
I am not familiar with JavaScript, but what if you save the PDF in the temp file first by calling you web service method, and then after it completed you can use JavaScript to navigate to the URL where generated PDF will be returned as content.
Since the XmlHTTPRequest Object cannot handle datatypes other than html, text, json, jsonp and xml you will need to redirect to the pdf location.
I'm not sure what exactly you're doing in your Ajax Request, but once that is completed you can just redirect the window(Form-Action) to the location of the created PDF. This wouldn't actually redirect the browser but only prompt for saving the file.
Why just create a function that returns the created PDF (during ajax request) as File result and then set the window's location to point to this action once the ajax request is completed successfully?
Edit: That means you are not saving the PDF anywhere. So the workaround, is to either use this jquery download plugin or append an iframe dynamically and then post the data through it. Hope this helps.
Figured out a workaround, what I did was write an extension to my HtmlHelper and use that to render my control into a html string instead of a view. Therefor I could use a actionlink to say render this page and get all the html data that way.
http://msug.vn.ua/Posts/Details/3301
Thanks for help tho!
Related
I am attempting to get the resulting web page content so I can extract the display text. I have attempted the code below but it gets me the source html and not the resulting html.
string urlPath = "http://www.cbsnews.com/news/jamar-clark-protests-follow-decision-not-to-file-charges-in-minneapolis-police-shooting/";
WebClient client = new WebClient();
string str = client.DownloadString(urlPath);
Compare the text in the str variable with the html in the Developer Tools in the Chrome browser and you will get different results.
Any recommendations will be appreciated.
I'm assuming you mean that you want the article text. If so you will need to follow a different course of action. The page you refer to is loaded with client script that injects loads of content into the base HTML document. This is done by executing the client-side script. You will need to parse the DOM after the script is executed to get the content you're interested in.
As others have pointed out, an actual web browser will parse the downloaded HTML and execute javascript against it, potentially altering its content. While you could try to do that parsing yourself, the easiest route is to ask a real web browser to do it for you and then grab the results.
The easiest solution specifically in C# would be to use the WebBrowser Control from Windows Forms, which essentially exposes IE to your program, allowing you to control it. Use the Navigate method to load the URL in question, then use the Document property to navigate the DOM. You can, at that point, get the outerHTML to get the final content of the DOM as HTML.
If you're not writing a Windows program and are interested more in headless operation, have a look at PhantomJS. It's a headless Webkit browser that is scriptable from javascript and would give you similar capability, although not in C#.
My requirement is to download a table that is present in the view in mvc
If I click a button, the table present in the view should be downloaded as excel file. In angularjs, I used option as ng-csv for this. But I don't know whether similar kind of option is present in mvc?
Could anyone help me with this ?
I'm not sure of what you mean by "a file that is present in the view".
If the user's file is generated server-side upon request, your button should link to an ActionMethod which returns a
FileContentResult
If you want the user's browser to convert the HTML page or a part of it to another format and save it, you should look into the html5 File API or a Javascript framework, not ASP.NET MVC.
How to extract the extra content loaded in a web page, which will not be visible in view page source. The extra content is being loaded using ajax. This data can be seen under NET tab using firebug. How to extract this data using c# code.
Two ways :
1- You can use webbrowser to load the same page and get the active document.
2- You can replicate the ajax call made, and use that to get the extra bits that are appended to the document.
And reading your linkedin example above:
When you select the checkbox a ajax call is made , which brings back results and populates the table.You can see that call using firebug console window and see the post parameter and replicate them to get the same result.
Depends on your application in the first place, if you are using c# application as the client for reading a web page, then the the ajax content may not be visible until you put in a javascript engine.
if you are serving the said pages, you only have to log the request response of the server.
More specific question would be appreciated
That extra content is dynamically generated by ajax (for eg: Gridview is generated as table), it is stored in browser's memory. and can be viewed by client side debugging tools (IE has developer tools option).
Once you do a post back, all the control's values are available for C#.
If you are saying extra content, can you please clarify what exactly you are trying to extract using c#?
I am trying to parse some images from this site..
I was using htmlagilitypack for the other pages
but this page uses ajax to load images
so this is how the webpage works.
has a div tag including nothing.
right below the div they have a script tag in cdata thing
<script type="text/javscript> //<
![CDATA[ (function(){
ajax POST request to a 'aaaaaa.js' file with the id parameter and if the request is success, it updates the blank div my changing the innerHTML value.
})(); //]]>
</script>
So.. what I tried was...
navigate to that page using the webbrowser control.
which loads the image just fine but when I try to get the value of 'DocumentText'
it only shows the blank div tag...
try to get straight data from the ajax POST using webrequest and webresponse..
but.. maybe cause its .js file.. it doesnt work.. I only get http errors.
Browse right to the js file with the parameters attached.
gives me an error page
Browse the page Im trying to parse and then navigate to the .js page.
(I guess the browser caches something when i browse the original page.. but i dont know what it is.)
I do get the json response! i can use this data.. but since the webbrowser control is using IE. It just asks me if i want to download the responsed js file.
So theres the method of using the DOM I mentioned in my comment.
Another method could be to use FiddlerCore ( http://www.fiddler2.com/fiddler/Core/)
Or make the ajax call yourself. You will have to make sure you respect cookies, redirects, and all the headers.
I have a controller with an action:
SomeController/ActionToBePrinted
ActionToBePrinted() returns an html view.
This action is called from a normal mvc razor view when pressing a button - how would I go about sending the content of the view to a printer when the button is pressed?
Aloha,
Hugo
You cant send direct to the printer.
I suggest you to create a custom ActionResult, that returns a PDF file or something like that. ASP.NET MVC Action Results and PDF Content
You can show a html page as well and open the print dialog using javascript like this
Click to Print This Page
But always the user has to start the print process, you cant do this programmatically.
You can perform a GET request (e.g. use window.open() and pass in URL or use AJAX) and put the returned HTML contents into a new window. Then use
Window.print(). Then simply close the window when you are done.
You could tie this directly into a single view by adding something in the body, but I prefer to use JavaScript in these cases. This keeps the design acting as a re-useable object or service that can be used across multiple views. In other words, you setup the controller-model, but no view. Instead, JavaScript steps in as the View.
Keep in mind that HTML is not a print format. So if you need to control the layout, you should be using a print technology such as PDF. XSLT provides an excellent means to create both HTML and PDF output using the same data, albeit it's a lot more work to create XSLT templates than it is to slap down window.print
Personally, I have an MVC page acting as a service that takes URL parameters. The page hooks into Adobe XSL-FO and uses the params to drive the output.