最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - HiQ PDF Conversion Issue: Body Text Overlapping with Header and Footer - Stack Overflow

programmeradmin3浏览0评论

I'm using HiQ PDF to convert my HTML content to a PDF document. Occasionally, I'm encountering an issue where the body text overlaps with the header and footer. This problem doesn't occur every time but happens sporadically. Below is the code I’m using.

View:

@model MyProjectLibrary.Model.PatientLetter

@{
    Layout = "~/Views/Shared/_LayoutPopup.cshtml";
}
<style>
    body {
        background-color: #FFF !important;
        color: black !important;
        font-size: 14px !important;
    }

    u {
        font-size: 22px !important;
    }
</style>

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Certificate</title>
</head>
<body>
    <div class="row">
        <div class="col-sm-10 col-sm-offset-1">
            @if (Model.LetterType != "Blank")
            {
                if (Model.LetterType == "Diagnosis Confirmation")
                {
                    <div class="row">
                        <div class="col-sm-12 text-center">
                            <label><u>To Whom It May Concern,</u></label>
                        </div>
                    </div>
                }
                else if (Model.LetterType == "GP")
                {
                    <div class="row">
                        <div class="col-sm-12 text-center">
                            <label><u> CONFIDENTIAL</u></label>
                        </div>
                    </div>
                }
                <div class="row">
                    <div class="col-sm-12">
                        <span style="margin-right: 10px;"><b style="font-size: 20px;">DATE:</b></span><span style="font-size: 20px;">@Html.Raw(Model.AppointmentDateStr)</span>
                    </div>
                </div>
                <div class="row top-margin-5">
                    <div class="col-sm-12">
                        <span style="margin-right: 10px;"><b style="font-size: 20px;">Re:</b></span><span style="font-size: 20px;">@Html.Raw(Model.modelPatient.FirstName) @Html.Raw(Model.modelPatient.SurName)</span>
                    </div>
                </div>
                <div class="row top-margin-5">
                    <div class="col-sm-12">
                        <span style="margin-right: 10px;"><b style="font-size: 20px;">DOB:</b></span><span style="font-size: 20px;">@Html.Raw(Model.modelPatient.DateOfBirthDateOnly)</span>
                    </div>
                </div>
                if (Model.LetterType == "GP")
                {
                    <div class="row top-margin-5">
                        <div class="col-sm-12">
                            <span style="margin-right: 10px;"><b style="font-size: 20px;">Dear Dr:</b></span><span style="font-size: 20px;">@Html.Raw(Model.modelPatient.modelGPPractice?.Name)</span>
                        </div>
                    </div>
                }
            }
            else
            {
                <div class="row">
                    <div class="col-sm-12">
                        <span style="margin-right: 10px;"><b style="font-size: 20px;">DATE:</b></span><span style="font-size: 20px;">@Html.Raw(Model.AppointmentDateStr)</span>
                    </div>
                </div>
            }
            <div class="row">
                <div class="col-sm-12">
                    <span style="font-size: 20px;">@Html.Raw(Model.LetterBody)</span>
                    <div class="row">
                        <div class="col-sm-6">
                            <span style="font-size: 20px;">Regards,</span><br />
                            <span style="font-size: 20px;">@Html.Raw(Model.modelUser.DoctorSignature)</span> <span style="font-size: 20px;">(electronically sighted and signed)</span>
                        </div>
                        <div class="col-sm-6">
                            <img src=".png" style="width:50% !important;" />
                        </div>
                    </div>
                </div>
            </div>
            <br />
        </div>
    </div>
</body>
</html>

Controller

public FileResult DownloadPatientLetter(Int64 letterId)
{
    PatientLetter model = objPatientLetterBLL.GetPatientLetterById(letterId);
    model.modelPatient = objPatientBLL.GetPatientByPatientId(model.PatientId);
    model.modelUser = objUserBLL.GetUserById(model.DoctorId);

    // create a new pdf document converting an url
    string htmlString = ViewToString(this, "ViewPatientLetter", model);
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
    htmlToPdfConverter.SerialNumber = "46u*****-ha+K*****-k****T-w9*****S-w9D*****-zd*****=";
    SetHeader(htmlToPdfConverter.Document);
    SetFooter(htmlToPdfConverter.Document);
    string baseUrl = ";;
    byte[] pdfBytes = htmlToPdfConverter.ConvertHtmlToMemory(htmlString, baseUrl);

    FileResult fileResult = new FileContentResult(pdfBytes, "application/pdf");
    fileResult.FileDownloadName = model.modelPatient.FirstName + " " + model.modelPatient.SurName + "_Correspondence_" + model.AppointmentDateMonthYear + ".pdf";

    if (!Convert.ToString(Session["_UserType"]).Equals("Super Admin"))
    {
        NotificationLogBLL objNotificationLogBLL = new NotificationLogBLL();
        objNotificationLogBLL.AddPortalLog(Convert.ToInt64(Session["_UserId"]), "Download Patient Letter at " + LocalDateTime.ConvertToLocalTimeZoneStr(DateTime.UtcNow) + " by " + Convert.ToString(Session["_FullName"]));
    }

    return fileResult;
}

static string ViewToString(Controller controller, string viewName, object model)
{
    controller.ViewData.Model = model;
    using (StringWriter sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
        ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
        viewResult.View.Render(viewContext, sw);
        return sw.ToString();
    }
}

private void SetHeader(PdfDocumentControl htmlToPdfDocument)
{
    // enable header display
    htmlToPdfDocument.Header.Enabled = true;

    if (!htmlToPdfDocument.Header.Enabled)
        return;

    // set header height
    htmlToPdfDocument.Header.Height = 180;

    float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ?
                                htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height;

    float headerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right;
    float headerHeight = htmlToPdfDocument.Header.Height;

    WebClient wc = new WebClient();
    byte[] bytes = wc.DownloadData(".PNG");
    MemoryStream ms = new MemoryStream(bytes);
    System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

    PdfImage logoHeaderImage = new PdfImage(0, 0, PdfPageSize.A4.Width, img);
    htmlToPdfDocument.Header.Layout(logoHeaderImage);

    PdfHtml headerHtml = new PdfHtml();
    headerHtml.FitDestHeight = true;
    headerHtml.FontEmbedding = true;
    htmlToPdfDocument.Header.Layout(headerHtml);
}

private void SetFooter(PdfDocumentControl htmlToPdfDocument)
{
    // enable footer display
    htmlToPdfDocument.Footer.Enabled = true;

    if (!htmlToPdfDocument.Footer.Enabled)
        return;

    // set footer height
    htmlToPdfDocument.Footer.Height = 45;

    float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ?
                                htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height;

    WebClient wc = new WebClient();
    byte[] bytes = wc.DownloadData(".JPG");
    MemoryStream ms = new MemoryStream(bytes);
    System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

    PdfImage logoFooterImage = new PdfImage(0, 0, PdfPageSize.A4.Width, img);
    htmlToPdfDocument.Footer.Layout(logoFooterImage);

    PdfHtml footerHtml = new PdfHtml();
    footerHtml.FitDestHeight = true;
    footerHtml.FontEmbedding = true;
    htmlToPdfDocument.Footer.Layout(footerHtml);
}

Issue: The body text sometimes overlaps with the header and footer in the generated PDF. Has anyone encountered a similar issue, and if so, how did you resolve it?

Any insights or suggestions would be greatly appreciated.

What I've Tried:

  • Adjusting the header and footer heights.
  • tried different margin values.

I need to resolve the issue of body text some time overlapping with the header/footer.

发布评论

评论列表(0)

  1. 暂无评论