This is for hybrid application which runs in android mobile webview container but has mvc code behind. That's why I cannot use window.open to show pdf.
I need to use iframe or embed may be to show it on the mvc view.
I get data as bytes from database.
byte[] RptData;
SqlReportData = reader.GetSqlBytes(0);
RptData = SqlReportData.Value;
return RptData;
then I convert it to Base64String and pass in viewModel.
viewModel.PdfDataString = Convert.ToBase64String(pdfData);
I tried pretty much top suggestions I could find but none of them working for me.
Iframe src set large base64 data
how to display base64 encoded pdf?
Display ASP.NET generated pdf byte[] to web page without saving the file
<div><iframe src="" type="application/pdf"></iframe>
</div>
<div>
<embed src="" width="100%" height="100%" type="application/pdf"/>
</div>
<script>
var pdfData = '#Model.PdfDataString';
$("iframe").attr("src", 'data:application/pdf;base64,{0}'.replace('{0}', pdfData));
$("embed").attr("src", 'data:application/pdf;base64,{0}'.replace('{0}', pdfData));
</script>
I want to show this Base64String data as pdf on mvc view.
I am trying with iframe and embed both.
iframe shows blank and embed is not displaying.
Chrome debugger shows:
Resource interpreted as Document but transferred with MIME type application/pdf: "data:application/pdf;base64,JVBERi0xLjcKCjQgMCB....(ShowMore 1.6MB)
Related
I have a classic ASP question.
I need to show images on my webpage.
But because of the security reasons, I cannot simply put all of my images in the images folder in my website folder and I need to put them outside of my website folder.
For example, my website folder is located at:
C:\Inetpub\wwwroot\mysite\
But I need to put images (which I want to show on my web pages) at:
C:\images\
I am trying to use ADODB.stream object to pull the images using ASP vb codes as shown below:
<%
Response.ContentType = "image/png"
Set adoStream = Server.CreateObject("ADODB.Stream")
adoStream.Open
adoStream.Type = 1
FPath = "C:\images\1.png"
adoStream.LoadFromFile FPath
Response.BinaryWrite adoStream.Read
adoStream.Close
Set adoStream = Nothing
Response.End
%>
When I launch this webpage in internet explorer, the page shows a File Download window/message for downloading the image file "1.png" rather than displaying the image on the web page.
How can I fix the code to show the image on the web page rather than downloading the image file?
That code generates an image as the return type, so there is no HTML to display the image in, if you call this code directly say it's called image.asp then you would do something like this in another page that displays HTML.
<img src="image.asp" />
This will call your code and output the raw image binary as this is what <img> expects.
If you want to produce multiple images from the file system or database pass a querystring parameter such as ?id=yourimage and change the code to select different images.
I'm writing a WinJS app that takes an HTML fragment the user has copied to the clipboard, replaces their
Later, when I go to display the .html, I create an iFrame element (using jQuery $(''), and attempt to source the .html into it, and get the following error
0x800c001c - JavaScript runtime error: Unable to add dynamic content. A script attempted to inject dynamic content, or elements previously modified dynamically, that might be unsafe. For example, using the innerHTML property to add script or malformed HTML will generate this exception. Use the toStaticHTML method to filter dynamic content, or explicitly create elements and attributes with a method such as createElement. For more information, see http://go.microsoft.com/fwlink/?LinkID=247104.
I don't get the exception if I don't base64 encoded the images, i.e. leave them intact and can display iframes on the page with the page showing images.
If I take the html after subbing the urls for base64 and run it through toStaticHTML, it removes the src= attribute completely from the tags.
I know the .html with the encoded pngs is right b/c I can open it in Chrome and it displays fine.
My question is I'm trying to figure out why it strips the src= attributes from the tags and how to fix it, for instance, creating the iframe without using jquery and some MS voodoo, or a different technique to sanitize the HTML?
So, a solution I discovered (not 100% convinced it the best and am still looking for something a little less M$ specific) is the MS Webview
http://msdn.microsoft.com/en-us/library/windows/apps/bg182879.aspx#WebView
I use some code like below (where content is the html string with base64 encoded images)
var loadHtmlSuccess = function (content) {
var webview = document.createElement("x-ms-webview");
webview.navigateToString(content);
assetItem.append(webview);
}
I believe you want to use execUnsafeLocalFunction. For example:
var target = document.getElementById('targetDIV');
MSApp.execUnsafeLocalFunction(function () {
target.innerHTML = content}
);
In our application we need to get the PDF from Application and should be displayed on IE9[8 or 7], Application is in ASP.Net MVC 3, the PDF file is coming from SSRS, based on user criteria SSRS will return PDF and that to be shown on browser in tag.
Things work perfect when we run application in debug mode in Visual Studio 2010 and application hosted in IIS7.5.
But as soon as we access application without debugger, PDF don't get rendered or loaded in Object Tag, not even Adobe AtiveX PDF Viewer appears.
So to figure out what is the issue, from IE9 developer toolbar captured network log and came to know that
URL :http://myhostname/Home/GetProfile/11
Result : Aborted
Type :
Initiator : ActiveX Control Initialization.
If I access the same application in Chrome, chrome loads PDF without any issue but IE9 is not able to load PDF.
Below is the code.
Criteria Page
<body>
<div>
<form id ="frmReport" method="get" action='#Url.Action("Report","Home")'
target="iReport">
<input type="text" />
<input type="submit" value="Click"/>
</form>
<iframe id="iReport" name="iReport"></iframe>
</div>
</body>
In above iReport iframe will get Result HTML which has object tag
Report Viewer html code.
<div id="reportContainer">
#{string urlPDF = "http://myhostname" + #Url.Action("GetProfile",
"Home", new { Id = "11" });
<object id = "pdfView" height="600px" width="100%"
type='application/pdf' data='#urlPDF'>
</object>}
</div>
Code in MVC application.
public FileStreamResult GetProfile(string Id)
{
var contentDisposition = new ContentDisposition
{
FileName = "Profile.pdf",
Inline = true
};
Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
byte[] bytes = ReportStore.GetProfileInByte(Id);
MemoryStream ms = new MemoryStream(bytes);
return new FileStreamResult(ms, MediaTypeNames.Application.Pdf);
}
Tried FileContentResult in place pf FileStreamResult and problem is same.
Do anyone know what is the problem with IE9? I traced the Application the request responded by server do not have any problem area, but IE9-PDF ActiveX is not handling content rendering, anyone knows what could be the reason?
Not only this but when I tried in our couple of machines the result is same disappointing.
Machines are having IE9 and AdobePDFReder 9\10. Everywhere with VS.Net Debugger things are working but once coming out from debugger things starts failing.
Thanks
I am newish to aspx pages but not to c#.
I have an SQL statement that will get images from ms sql database and I can do that fine and have a list or datatable of byte arrays for the images.
I think I want to use a
The part where I'm most confused is how to display a byte array as an image?
Thank you.
You need to write an handler (*.ashx) to do the job of getting an image's bytes and writing them to the response stream. Then you reference this handler in the src="" attribute of your . Here's a tutorial on how to do this:
http://www.developerfusion.com/code/5223/using-ashx-files-to-retrieve-db-images/
You could use inline images if image in DB is rather small.
// This is just example, you get it from DB
byte[] image = System.IO.File.ReadAllBytes(#"c:\\test.png");
string imageBase64 = Convert.ToBase64String(image);
...
// Output in your HTML page
<img src="data:image/png;base64,<%= imageBase64 %>" alt="Test inline" />
I have the following acceptance criteria for creating a pdf file from my asp.net page which contains nested RadGrid controls:
The current view of the page should be converted to PDF which means that the viewstate and session information of the current page request should be taken into account. This leaves me with only one option; make the PDF conversion at Page_Render() event handler of the current session when a new pdf postback is sent.
The asp.net page layout is changed using JQuery at the time of the $(document).ready(...) that means that not only the rendered HTML should be converted to PDF but also the javascripts have to run on it to have the desired layout changes in the final PDF file; e.g. column alignments, etc. I hope it would be possible otherwise ...
The asp.net page only appears correctly in IE 6+ therefore the PDF tool which is used must use IE rendering engine.
Please could you advise which tool can help in such scenario?
I downloaded and tested EvoPdf tool but it doesn't support IE rendering engine apparently (only FireFox rendering) and couldn't make the javascripts enabling work correctly with it.
I'm going to evaluate ABCPdf and Winnovetive but I'm not sure they would support what I want.
If I could find no tool to help with the above, another possible solution might be just taking a screenshot of the page using client script (don't know whether it'd be possible), then sending it to the server and finally converting that image to pdf.
Many thanks,
You can try WebToPDF.NET.
Try to convert HTML page which you get after the asp.net page have been rendered
WebToPDF.NET suports JavaScript(and JQuery), so it's not problem
WebToPDF.NET passes all W3C tests (except BIDI) and supports HTML 4.01, JavaScript, XHTML 1.0, XHTML 1.1 and CSS 2.1 including page breaks, forms and links.
Don't know exactly about your requirements but have a look at wkhtmltopdf
How to use wkhtmltopdf.exe in ASP.net
winnovative did exactly what I needed :) it uses IE rendering engine unlike EvoPdf.
I haven't had time testing other tools.
Thanks
EvoPdf is developed by the same team who develop ExpertPDF (http://www.html-to-pdf.net/). ExpertPDF is the older product so although the APIs are almost identical, the EvoPDF API is slightly more refined.
The main difference between the products is that ExpertPDF uses the local IE rendering engine.
Winnovative HTML to PDF Converter does not use IE as rendering engine. It is compatible with WebKit rendering and does not depend on IE or any other third party tools.
You can convert the current HTML page overriding the Render() method of the ASP.NET page and capture the HTML code being rendered by page. You can find complete example with C# source code in Convert the Current HTML Page to PDF Demo.
Here is the relevant source code for this approach:
// Controls if the current HTML page will be rendered to PDF or as a normal page
bool convertToPdf = false;
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// The current ASP.NET page will be rendered to PDF when its Render method will be called by framework
convertToPdf = true;
}
protected override void Render(HtmlTextWriter writer)
{
if (convertToPdf)
{
// Get the current page HTML string by rendering into a TextWriter object
TextWriter outTextWriter = new StringWriter();
HtmlTextWriter outHtmlTextWriter = new HtmlTextWriter(outTextWriter);
base.Render(outHtmlTextWriter);
// Obtain the current page HTML string
string currentPageHtmlString = outTextWriter.ToString();
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";
// Use the current page URL as base URL
string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;
// Convert the current page HTML string a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(currentPageHtmlString, baseUrl);
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Current_Page.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
else
{
base.Render(writer);
}
}