Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Want save (download) navigated Map as Image which contain root navigated also visited spot but when I am saving it as image then it is showing only navigated line
it is not showing map.

What I have tried:

written Javascript function WHich take Id of div on which navigated map is present as follows


<script type="text/javascript" src="http://cdn.rawgit.com/niklasvh/html2canvas/master/dist/html2canvas.min.js"></script>
   
    
    

    <script type="text/javascript">
        function ConvertToImage(btnExportPdf) {
            debugger;
            alert("Hi");
            html2canvas($("#MyWhiteBoxMap")[0]).then(function (canvas) {
                debugger;
                var base64 = canvas.toDataURL();
                $("[id*=hfImageData]").val(base64);
                debugger;
                __doPostBack(btnExportPdf.name, "");

            });
            return false;
        }
</script>  



then on button click I am calling function

<asp:Button ID="btnExportPdf" runat="server" CssClass="btn btn-default" Text="To PDF Report" OnClientClick="Export()" OnClick="btnExportPdf_Click" ToolTip="Export Excel" />


server button click code is as follows

protected void btnShowPrint_Click(object sender, System.EventArgs e)
   {


       //Dummy data for Invoice (Bill).
       string companyName = "";
       int orderNo = 2303;
       DataTable dt = new DataTable();
       if (ViewState["RecordData"] != null) { dt = (DataTable)ViewState["RecordData"]; }
       divReportInput.Visible = false;
       backgroundDiv.Visible = false;
       using (System.IO.StringWriter sw = new System.IO.StringWriter())
       {
           using (HtmlTextWriter hw = new HtmlTextWriter(sw))
           {
               System.Text.StringBuilder sb = new System.Text.StringBuilder();
               System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
               //Generate Invoice (Bill) Header.
               sb.Append("<div style='text-align:center'>Report</div>");
               sb.Append("<table  style='width:100%'>");
               sb.Append(" <tr> <td >Device Name</td><td >: " + txtDeviceName.Text + "</td><td></td><td></td> </tr>");
               sb.Append(" <tr></td>");
               sb.Append("<td >Test Name</td> <td >: " + txtTestName.Text + "</td>");
               sb.Append("  <td >Report Generated by</td><td>: " + txtReportGeneratedby.Text + "</td>");
               sb.Append("</tr>");
               sb.Append(" <tr>");
               sb.Append(" <td >Start Date</td> <td >: " + txtStartDate.Text + "</td>");
               sb.Append("<td >End Date</td> <td >: " + txtEndDate.Text + "</td>");
               sb.Append("</tr>");
               sb.Append(" </table>");

               sb2.Append("<div style='text-align:center'>ROOT MAP</div>");
               //Export HTML String as PDF.
               System.IO.StringReader sr = new System.IO.StringReader(sb.ToString());
               System.IO.StringReader sr2 = new System.IO.StringReader(sb2.ToString());
               byte[] file;
               file = System.IO.File.ReadAllBytes(Server.MapPath("img/A.png"));//ImagePath
               iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(file);
               png.ScaleToFit(800F, 500F);//Set width and height in float


               //Addeded By vinayak

               //Capture(Server.MapPath("img/MapImage.jpg"));





               string base64 = Request.Form[hfImageData.UniqueID].Split(',')[1];
               byte[] bytes = Convert.FromBase64String(base64);
               Response.Clear();
               Response.ContentType = "image/png";
               Response.AddHeader("Content-Disposition", "attachment; filename=HTML.png");
               Response.Buffer = true;
               Response.Cache.SetCacheability(HttpCacheability.NoCache);
               Response.BinaryWrite(bytes);
               MemoryStream storeStream = new MemoryStream();
               MemoryStream ms = new MemoryStream(bytes);
               //write to file
               FileStream file1 = new FileStream(Server.MapPath("img/HTML100.png") , FileMode.Create,
               FileAccess.Write);
               ms.WriteTo(file1);
               file1.Close();
               ms.Close();
               Response.End();




               //Addeded By vinayak

               //takeComponentScreenShot(backgroundDiv);
               ////Map Img.......
               //byte[] fileMap;
               //fileMap = System.IO.File.ReadAllBytes(Server.MapPath("img/MapImage.jpg"));//ImagePath
               //iTextSharp.text.Image pngMap = iTextSharp.text.Image.GetInstance(fileMap);
               //pngMap.ScaleToFit(800F, 500F);//Set width and height in float


               iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
               iTextSharp.text.html.simpleparser.HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc);
               iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
               pdfDoc.Open();
               htmlparser.Parse(sr);
               pdfDoc.Add(png);
               htmlparser.Parse(sr2);
               //pdfDoc.Add(pngMap);
               pdfDoc.Close();
               Response.ContentType = "application/pdf";
               Response.AddHeader("content-disposition", "attachment;filename=Record Report.pdf");
               Response.Cache.SetCacheability(HttpCacheability.NoCache);
               Response.Write(pdfDoc);
               Response.End();

           }
       }

   }
Posted
Updated 1-Oct-18 9:43am
v2
Comments
Richard Deeming 2-Oct-18 11:37am    
function ConvertToImage(btnExportPdf) {

OnClientClick="Export()"


The function names don't match. And your client-side function will need to call preventDefault to prevent the button from posting the form before your canvas has been drawn.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900