When trying to capture and convert HTML DIV to image, the button click event does not fire immediately, I always have to click the button twice or more in order for it to work.
May I please ask why button click does not fire immediately after it is clicked, in this regard?
How can this be corrected please?
HTML
<asp:Label ID="LabelName" runat="server" Text="Richard"></asp:Label>
<asp:Image Class="imgfile" ID="imgfile" runat="server" />
<br />
<asp:Button ID="SaveDoc" runat="server" CssClass="btn btn-primary navbar-btn" Text="Save Document"
OnClick="DownloadDocument_Click" UseSubmitBehavior="false" OnClientClick="return ConvertDocument(this)" />
<asp:HiddenField ID="ImageDatahf" runat="server" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<script type="text/javascript">
function ConvertDocument(SaveDoc) {
html2canvas($("#section")[0], { scale: 3 })
.then(function (canvas) {
var base64 = canvas.toDataURL();
$("[id*=ImageDatahf]").val(base64);
__doPostBack(SaveDoc.name, "");
});
}
</script>
Code
protected void DownloadDocument_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string base64 = Request.Form[ImageDatahf.UniqueID].Split(',')[1];
byte[] imageBytes = Convert.FromBase64String(base64);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageBytes);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
if (Convert.ToInt64(image.Width.ToString()) > Convert.ToInt64(image.Height.ToString()))
{
using (Document document = new Document(new RectangleReadOnly(842, 595), 0f, 0f, 0f, 0f))
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
image.SetDpi(100, 100);
image.ScaleToFit(500f, 1000f);
document.Add(image);
document.Close();
}
}
else
{
using (Document document = new Document(new RectangleReadOnly(595, 842), 0f, 0f, 0f, 0f))
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
image.SetDpi(100, 100);
image.ScaleToFit(500f, 1000f);
document.Add(image);
document.Close();
}
}
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
// Code for saving binary data in daabase.
using (SqlCommand cmd = new SqlCommand("INSERT INTO DocumentTable (filename, ContentType, Data, Recipient)" + "VALUES(@filename, @ContentType, @Data, @Recipient)", con))
{
cmd.Parameters.AddWithValue("@filename", "Certificate.pdf");
cmd.Parameters.AddWithValue("@ContentType", "application/pdf");
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Parameters.AddWithValue("@Recipient", LabelName.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(Request.Url.AbsoluteUri);
}
}
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}