Hello, I wrote a pdf code in asp.net mvc, but it works when there is no database, it does not work when there is a database. I am sending the codes.
I would be very happy if you find a solution.
using Rotativa;
public ActionResult Index()
{
var ad = (string)Session["FIRMAKODU"];
var plasiyerid = (string)Session["PLASIYERID"];
var kullaniciid = (string)Session["KULLANICIADI"];
ViewBag.ad = ad;
ViewBag.plasiyerid = plasiyerid;
var anamenu = db.WEBAYARLAR.Where(x => x.FIRMAKODU == ad & x.PLASIYERID == plasiyerid & x.KULLANICIADI == kullaniciid).ToList();
if (ad != null)
{
return View(anamenu);
}
else
{
return RedirectToAction("Login", "Menu");
}
}
[HttpPost]
public ActionResult GeneratePdf()
{
var htmlContent = "Merhaba Samet"; // PDF'ye dönüştürmek istediğiniz metin
var pdfBytes = GeneratePdfBytes(htmlContent);
return File(pdfBytes, "application/pdf", "Belge.pdf");
}
private byte[] GeneratePdfBytes(string htmlContent)
{
var pdf = new ViewAsPdf
{
ViewName = "Index",
IsGrayScale = false,
FileName = "Belge.pdf",
PageSize = Rotativa.Options.Size.A4,
PageMargins = new Rotativa.Options.Margins(10, 10, 10, 10)
};
return pdf.BuildFile(ControllerContext);
}
@using samkasaha.Models.Siniflar
@model List<WEBAYARLAR>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/View.cshtml";
}
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
@foreach (var a in Model)
{
<h1>@a.FIRMAADI</h1>
<h1>@a.KULLANICIADI</h1>
<h1>@a.GUNCELLEME</h1>
<img src="@a.LOGO" />
}
<div id="pdfDiv" style="display: none;">
@using (Html.BeginForm("GeneratePdf", "Menu", FormMethod.Post))
{
<input type="submit" value="PDF Oluştur" />
}
</div>
<div id="htmlDivContent">
@foreach (var a in Model)
{
<h1>@a.FIRMAADI</h1>
<h1>@a.KULLANICIADI</h1>
<h1>@a.GUNCELLEME</h1>
<img src="@a.LOGO" />
}
</div>
<script>
$(document).ready(function () {
// PDF div'inin görünürlüğünü kontrol et
function checkPdfVisibility() {
var pdfDivVisible = $("#pdfDiv").is(":visible");
$("#htmlDivContent").toggle(!pdfDivVisible);
}
// Sayfa yüklendiğinde PDF div'i gizli
checkPdfVisibility();
// PDF div'inin görünürlüğünü kontrol et ve içeriği gizle
$("#pdfDiv").show(function () {
checkPdfVisibility();
});
});
</script>