Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed May 26, 2021 11:22 pm

Hello!

I need a help. In .net Core 3.1 (using docker Linux) I receive a message:

"The type initializer for 'spr?' threw an exception." -> "Cannot found font installed on the system."

My Code:

using Spire.Pdf;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using Spire.Pdf.HtmlConverter;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;

namespace assinatura.documento.api.netcore.Util
{
[ExcludeFromCodeCoverage]
public static class PdfUtil
{
public static string InsereImagemNoPdf(string base64Documento, string base64Imagem, float larguraImg, float alturaImg, string textoSubs = "", bool textInicio = true, float fatorCorrecaoX = 0, float fatorCorrecaoY = 0)
{
return InsereImagemNoPdf(base64Documento, base64Imagem, larguraImg, alturaImg, null, null, textoSubs, textInicio, fatorCorrecaoX, fatorCorrecaoY);
}

public static string InsereImagemNoPdf(string base64Documento, string base64Imagem, float larguraImg, float alturaImg, float? posicaoX, float? posicaoY, string textoSubs = "", bool textInicio = true, float fatorCorrecaoX = 0, float fatorCorrecaoY = 0)
{
ConstroiGridRow(base64Documento, out var nomeArquivoPdf, out var pdfDocument, out var pdfPageBase, out var pdfGrid, out var pdfGridRow);

RealizaCalculoTamanhos(base64Imagem, larguraImg, alturaImg, pdfPageBase, pdfGrid, out var pdfGridCellContentList, out var nomeArquivoImg);

CalculaCelulas(pdfGridRow, pdfGridCellContentList);

if ((posicaoX.HasValue && posicaoY.HasValue) || string.IsNullOrEmpty(textoSubs))
return SalvaNoArquivo(posicaoX.Value, posicaoY.Value, nomeArquivoPdf, pdfDocument, pdfPageBase, pdfGrid,
nomeArquivoImg);

var pointF = ReturnPositionTextPdf(pdfDocument, textoSubs, textInicio);
posicaoX = pointF.X + fatorCorrecaoX;
posicaoY = pointF.Y + fatorCorrecaoY;

return SalvaNoArquivo(posicaoX.Value, posicaoY.Value, nomeArquivoPdf, pdfDocument, pdfPageBase, pdfGrid, nomeArquivoImg);
}

public static string ConverteStringHtmlToBase64Html(string stringHtml)
{
var htmlToPdf = new SelectPdf.HtmlToPdf();
if (!Directory.Exists("Arquivos"))
Directory.CreateDirectory("Arquivos");

if (!Directory.Exists(@"Arquivos\Conversoes"))
Directory.CreateDirectory(@"Arquivos\Conversoes");

var nomeArquivoSalvo = @$"Arquivos\Conversoes\{Guid.NewGuid()}.html";

var htmlString = htmlToPdf.ConvertHtmlString(stringHtml);
htmlString.Save(nomeArquivoSalvo);

var pdfDocument = new PdfDocument(nomeArquivoSalvo);

using var memoryStream = new MemoryStream();
pdfDocument.SaveToStream(memoryStream);
var bytes = memoryStream.ToArray();

File.Delete(nomeArquivoSalvo);

var base64String = Convert.ToBase64String(bytes);

return base64String;
}

private static string SalvaNoArquivo(float posicaoX, float posicaoY, string nomeArquivoPdf, PdfDocument pdfDocument, PdfPageBase pdfPageBase, PdfGrid pdfGrid, string nomeArquivoImg)
{
pdfGrid.Draw(pdfPageBase, new PointF(posicaoX, posicaoY));

using var memoryStream = new MemoryStream();
pdfDocument.SaveToStream(memoryStream);
var bytes = memoryStream.ToArray();

File.Delete(nomeArquivoImg);
File.Delete(nomeArquivoPdf);

return Convert.ToBase64String(bytes);
}

private static void CalculaCelulas(PdfGridRow pdfGridRow, PdfGridCellContentList pdfGridCellContentList)
{
pdfGridRow.Cells[0].Value = pdfGridCellContentList;
pdfGridRow.Cells[0].Style.Borders.Top.Color = PdfRGBColor.Empty;
pdfGridRow.Cells[0].Style.Borders.Bottom.Color = PdfRGBColor.Empty;
pdfGridRow.Cells[0].Style.Borders.Left.Color = PdfRGBColor.Empty;
pdfGridRow.Cells[0].Style.Borders.Right.Color = PdfRGBColor.Empty;
}

private static void RealizaCalculoTamanhos(string base64Imagem, float larguraImg, float alturaImg, PdfPageBase pdfPageBase, PdfGrid pdfGrid, out PdfGridCellContentList pdfGridCellContentList, out string nomeArquivoImg)
{
pdfGridCellContentList = new PdfGridCellContentList();
PdfGridCellContent pdfGridCellContent = new PdfGridCellContent();
var byteImagem = Convert.FromBase64String(base64Imagem);
if (!Directory.Exists("Arquivos"))
Directory.CreateDirectory("Arquivos");

if (!Directory.Exists(@"Arquivos\Conversoes"))
Directory.CreateDirectory(@"Arquivos\Conversoes");
nomeArquivoImg = @$"Arquivos\Conversoes\{Guid.NewGuid()}.png";
File.WriteAllBytes(nomeArquivoImg, byteImagem);

pdfGridCellContent.Image = PdfImage.FromFile(nomeArquivoImg);

pdfGridCellContent.ImageSize = new SizeF(larguraImg, alturaImg);
pdfGridCellContentList.List.Add(pdfGridCellContent);
}

private static void ConstroiGridRow(string base64Documento, out string nomeArquivoPdf, out PdfDocument pdfDocument, out PdfPageBase pdfPageBase, out PdfGrid pdfGrid, out PdfGridRow pdfGridRow)
{
var bytesPdf = Convert.FromBase64String(base64Documento);
if (!Directory.Exists("Arquivos"))
Directory.CreateDirectory("Arquivos");

if (!Directory.Exists(@"Arquivos\Conversoes"))
Directory.CreateDirectory(@"Arquivos\Conversoes");
nomeArquivoPdf = @$"Arquivos\Conversoes\{Guid.NewGuid()}.pdf";
File.WriteAllBytes(nomeArquivoPdf, bytesPdf);

pdfDocument = new PdfDocument(bytesPdf);
pdfPageBase = pdfDocument.Pages[0];
pdfGrid = new PdfGrid();
pdfGridRow = pdfGrid.Rows.Add();
pdfGrid.Columns.Add(1);
}

public static PointF ReturnPositionTextPdf(PdfDocument document, string word, bool inicio = true)
{
var finds = document.Pages[0].FindText(word, TextFindParameter.None).Finds;
return inicio ? finds.FirstOrDefault().Position : finds.LastOrDefault().Position;
}

public static string JuntarDoisEmUm(string base64Documento1, string base64Documento2)
{
var bytesPdf1 = Convert.FromBase64String(base64Documento1);
if (!Directory.Exists("Arquivos"))
Directory.CreateDirectory("Arquivos");

if (!Directory.Exists(@"Arquivos\Conversoes"))
Directory.CreateDirectory(@"Arquivos\Conversoes");

var nomeArquivoPdf1 = @$"Arquivos\Conversoes\{Guid.NewGuid()}.pdf";
File.WriteAllBytes(nomeArquivoPdf1, bytesPdf1);

var pdfDocumentFinal = new PdfDocument(bytesPdf1);

var bytesPdf2 = Convert.FromBase64String(base64Documento2);
var nomeArquivoPdf2 = @$"Arquivos\Conversoes\{Guid.NewGuid()}.pdf";
File.WriteAllBytes(nomeArquivoPdf2, bytesPdf2);

var pdfDocument2 = new PdfDocument(bytesPdf2);


pdfDocumentFinal.AppendPage(pdfDocument2);

using var memoryStream = new MemoryStream();
pdfDocumentFinal.SaveToStream(memoryStream);
var bytes = memoryStream.ToArray();

File.Delete(nomeArquivoPdf1);
File.Delete(nomeArquivoPdf2);

return Convert.ToBase64String(bytes);
}
}
}

rubvitor
 
Posts: 3
Joined: Wed May 26, 2021 11:15 pm

Thu May 27, 2021 9:16 am

Hello,

Thanks for your inquiry.

Since there are some custom methods in the code you provided, it couldn’t be run directly. I made some changes to it and then tested, but I failed to reproduce your issue.

From your error message, it seems that the issue is due to a font not being installed on your system. Please kindly note that when using our Spire.PDF to process PDF, you need to make sure that your system has installed the fonts used in the PDF. Please check if you have installed the corresponding fonts in the docker container.

If the issue still occurs after installing the fonts, please provide your input file, your complete code (or a runnable project) along with your dockerfile to help us investigate further. You could send them to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu May 27, 2021 1:20 pm

Helo! Thank you!

I didn't use any specific font in my code, you can see it by analyzing the code.

rubvitor
 
Posts: 3
Joined: Wed May 26, 2021 11:15 pm

Fri May 28, 2021 3:54 am

Hello,

Thanks for your response.

Yes, I have checked the code you provided and did notice that there is nothing about setting the font. But there is a custom method “SelectPdf.HtmlToPdf()”, and you did not give a specific definition. About this method, are you using our product to convert html to PDF? If so, you need to ensure that all fonts used in html are installed in your system.

Since your code cannot be run directly, to help us reproduce your issue and help you solve it, please provide your input file, your complete code (or a runnable project) along with your dockerfile to help us investigate further. You could send them to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu Jun 03, 2021 11:36 am

Hello

How is your issue now? Could you please give us some feedback at your convenience?

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF