我有一个页面创建一个Itext pdf文档,然后下载文档。以下代码就是这样做的。但页面刷新。我想页面不必刷新。我认为这将提供更好的用户体验,使页面表单输入不会在页面刷新时消失。
是否有更好的方法来处理此任务,以便不必刷新页面?是否可以对现有代码进行一些修改以避免页面刷新?
labelReprint.js:
$.ajax({
url: "labelReprint.aspx/DoLabelPrint",
data: "{'type':'" + lblType + "','scan':'" + scan + "','qty':'" + qty + "','split':'" + split + "'}",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
console.log('Downloading .pdf');
//pdf
var downloadpdf = $('<a id="downloadpdf" download="' + fileName + '.pdf" href="data:application/pdf;base64,' + result.d + '" >');
$('#download').append(downloadpdf);
document.getElementById("downloadpdf").click();
$("#downloadpdf").remove();
},
error: function (result) {
console.log(result.responseJSON.Message + "\n\r\n\r" + result.responseJSON.StackTrace);
}
});
从上面的AJAX中引用的labelReprint.aspx / DoLabelPrint调用follow函数。这将处理Itext / pdf创建。
Public Shared Function mfgrLabel(ByVal barCode As String, ByVal user As String) As String
Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim subs = New subs
Dim pSize As New iTextSharp.text.Rectangle(252, 36)
Dim pdfDoc As New iTextSharp.text.Document(pSize, 0.0F, 0.0F, 0.0F, 0.0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, MS)
pdfDoc.Open()
Dim cb As PdfContentByte = writer.DirectContent
Dim tblLbl As iTextSharp.text.pdf.PdfPTable = New iTextSharp.text.pdf.PdfPTable(2)
Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, False)
Dim tRsmall As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 5, iTextSharp.text.Font.NORMAL)
Dim tR As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 6, iTextSharp.text.Font.NORMAL)
Dim tRMed As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 7, iTextSharp.text.Font.NORMAL)
Dim tRMedLrg As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL)
Dim tRLrg As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL)
Dim tXRLrg As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL)
Dim cBlankNB As iTextSharp.text.pdf.PdfPCell = New iTextSharp.text.pdf.PdfPCell
cBlankNB.Border = 0
tblLbl.HorizontalAlignment = 0
'*************** Labels ***************************
Dim cellTimestamp As iTextSharp.text.pdf.PdfPCell = New iTextSharp.text.pdf.PdfPCell(New iTextSharp.text.Phrase(barCode, tR)) '"Inovar P/N:"
Dim cPartNo As iTextSharp.text.pdf.PdfPCell = New iTextSharp.text.pdf.PdfPCell(New iTextSharp.text.Phrase(barCode.Split("|")(0) & " " & barCode.Split("|")(1), tRMedLrg))
Dim uInfo As String = user.ToUpper & " " & Now.ToString("dd-MMM-yyyy").ToUpper
Dim pInfo As String = barCode.Split("|")(0) & " " & barCode.Split("|")(1)
cellTimestamp.Border = 0
cPartNo.Border = 0
'**************************************************
'************ Bar Codes **************************
Dim safeFileName As String = subs.fixFileNameBeforeSave(barCode.Split("|")(1))
Dim inoDm As Barcodes.DataMatrix.DataMatrixWebControl = New Barcodes.DataMatrix.DataMatrixWebControl
inoDm.DataToEncode = barCode.Split("|")(1)
If File.Exists(HttpContext.Current.Server.MapPath(safeFileName & ".jpg")) Then
File.Delete(HttpContext.Current.Server.MapPath(safeFileName & ".jpg"))
End If
inoDm.SaveToImageFile(25, 25, HttpContext.Current.Server.MapPath(safeFileName & ".jpg"), 300, Barcodes.DataMatrix.Dimensions.dmMM)
Dim inoDmImg As iTextSharp.text.Image
inoDmImg = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(safeFileName & ".jpg"))
inoDmImg.ScaleAbsolute(25, 25)
Dim inoPBC As iTextSharp.text.pdf.PdfPCell
inoPBC = New iTextSharp.text.pdf.PdfPCell(iTextSharp.text.Image.GetInstance(inoDmImg))
inoPBC.VerticalAlignment = 6
inoPBC.HorizontalAlignment = 0
inoPBC.Border = 0
inoPBC.PaddingLeft = 10.0F
inoPBC.PaddingTop = 15.0F
'************ End Bar Codes **************************
inoPBC.PaddingTop = 5.0F
cPartNo.NoWrap = True
inoPBC.Colspan = 2
tblLbl.AddCell(inoPBC)
pdfDoc.Add(tblLbl)
cb.SetFontAndSize(bf, 8)
cb.BeginText()
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, pInfo, 40, 20, 0) '180
cb.EndText()
cb.SetFontAndSize(bf, 4)
cb.BeginText()
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, uInfo, 40, 4, 0) '180
cb.EndText()
pdfDoc.Close()
'Clean up
If File.Exists(HttpContext.Current.Server.MapPath(safeFileName & ".jpg")) Then
File.Delete(HttpContext.Current.Server.MapPath(safeFileName & ".jpg"))
End If
Return System.Convert.ToBase64String(MS.ToArray())
任何想法或见解都将非常受欢迎。
答案 0 :(得分:0)
在Ajax的完成中,输入以下代码:
var binaryString = window.atob(result);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var idx = 0; idx < binaryLen; idx++) {
var ascii = binaryString.charCodeAt(idx);
bytes[idx] = ascii;
}
var blob = new Blob([bytes]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
var fileName = "filename.pdf";
link.download = fileName;
link.click();