iTextSharp image not showing in aws lambda for c# - c#

I'm adding an image to a iTextSharp pdf in this way:
iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4, 0F,0F, 0F, 0F);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("https://s3-eu-west-1.amazonaws.com/foo/bar.png");
img.ScaleToFit(595, 120);
MemoryStream ms = new MemoryStream();
PdfWriter.GetInstance(doc, ms);
doc.Open();
doc.Add(img);
doc.Close();
Then I convert it to byte[] and then to base64 string, so it can be handled by AWS API Gateway.
byte[] pdf = ms.ToArray();
var headersDic = new Dictionary<string, string>();
headersDic.Add("Content-type", "application/pdf");
headersDic.Add("Content-disposition", "inline;filename=file.pdf");
return new APIGatewayProxyResponse
{
Body = Convert.ToBase64String(pdf),
IsBase64Encoded = true,
Headers = headersDic,
StatusCode = 200
};
But the image is not shown in the pdf, just an almost indistinguishable part of it, which is the line you can see here:
Any ideas? maybe the conversions to byte[] or base64 and to binary again are giving problems?
When I add text everything goes good.

Related

Conver itext7 PdfDocument to byte array in c#

I am using itext7 pdfhtml (4.0.3) to convert Html to pdf in memory. Below method is taking html in memory and returning PdfDocument object of itext7. I need to convert that PdfDocument object to byte array or stream.
Please let me know how we can achieve that.
private iText.Kernel.Pdf.PdfDocument CreatePdf( string html)
{
byte[] bytes = Encoding.ASCII.GetBytes(html);
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(path);
MemoryStream myMemoryStream = new MemoryStream(bytes);
PdfWriter writer = new(myMemoryStream);
iText.Kernel.Pdf.PdfDocument pdf = new iText.Kernel.Pdf.PdfDocument(writer);
pdf.SetDefaultPageSize(PageSize.A4);
pdf.SetTagged();
HtmlConverter.ConvertToDocument(html,pdf,properties);
return pdf;
}
to #mkl's point, you're kind of overdoing it. Here's a simple example:
var html = "<h1>hi mom</h1>";
byte[] result;
using (var memoryStream = new MemoryStream())
{
var pdf = new PdfDocument(new PdfWriter(memoryStream));
pdf.SetDefaultPageSize(PageSize.A4);
pdf.SetTagged();
HtmlConverter.ConvertToPdf(html, pdf, new ConverterProperties());
result = memoryStream.ToArray();
}
File.WriteAllBytes(#"/tmp/file.pdf", result);
the memoryStream will have your in memory representation of your conversion. I've added the WriteAllBytes bits just so you can see for yourself.
Another note, if you do not require setting any PdfDocument properties, you can use an even simpler version:
var html = "<h1>hi mom</h1>";
byte[] result;
using (var memoryStream = new MemoryStream())
{
HtmlConverter.ConvertToPdf(html, memoryStream);
result = memoryStream.ToArray();
}
File.WriteAllBytes(#"/tmp/file.pdf", result);

iText7 create a document from IFormFile

I want to insert image to the user upload file, when I tried to create pdfDocument from the stream I got the "You can't read from OutputStream" error
public ActionResult GetTemplate(IFormFile template)
{
byte[] pdfBytes;
using (var memoryStream = new MemoryStream())
{
template.CopyTo(memoryStream);
var writer = new PdfWriter(memoryStream);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(memoryStream));
var document = new Document(pdfDocument);
var pageCount = pdfDocument.GetNumberOfPages();
ImageData imageData = ImageDataFactory.CreatePng(System.Convert.FromBase64String(//));
var img = new Image(imageData).Scale(200, 100).SetFixedPosition(pageCount, 50, 120);
document.Add(img);
document.Close();
pdfBytes = memoryStream.ToArray();
}
return new FileContentResult(pdfBytes, "application/pdf");
}
If I changed it to new PdfDocument(new PdfReader(writer)); I got "PDF header not found" error instead.
This is my first time using iText, most of the answers I found on their website only worked with java. Any suggestion or help would be great, thank you for your time.

Display Chinese Text in iTextSharp pdf from html c#

I want to display chinese text from html to pdf by using itextsharp in c#
Text in HTML is properly visible but when i tried to make pdf from Xml Parser in iTextSharp it does not show me chinese texts.
UTF8 encoding is not working properly. I also given Encoding.UTF8 but it also not worked.
Below are my code to generate PDF from html.
public static byte[] HtmlToPDFConvert(string baseHtml, Rectangle pageSize)
{
Stream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(baseHtml ?? ""));
Document pdfDoc = new Document(pageSize, 18f, 18f, 18f, 18f);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, htmlStream, null, Encoding.UTF8, FontFactory.FontImp);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
return bytes;
}
}
Since Xmlworker has been deprecated by pdfHTML, I've used it instead.
The only trick is to point to a font that supports the glyphs you want to use.
ConverterProperties props = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider(true, true, true);
fontProvider.AddFont("fonts/NotoSansCJKjp-Regular.otf");
props.SetFontProvider(fontProvider);
PdfDocument doc = new PdfDocument(new PdfWriter(DEST));
HtmlConverter.ConvertToPdf(new FileStream(ORIG, FileMode.Open), doc, props);

Trying to debug some iTextSharp code that is designed to convert an web page into a PDF document via a string variable

I am stuck trying to debug some code that is designed to convert an web page into a PDF document via a string variable. It uses the iTextSharp c# tool (xmlworker) and is a modification of the example code that comes with the sourceforge source code called html2pdf.csproj. This example code converts an existing html file into a PDF file and saves it in the same directory as the file that was converted. I have a string variable containing html formatted text and I am trying to make it able to be returned as a byte array that will be passed to the client side in a web environment for printing purposes. The problem is that I get an "IOException was unhandled by user code" message that states "The document has no pages." I'm a little unsure what this is suppose to mean, nor how to go about diagnosing the problem. The example code using the file based system works and I have successfully converted a static version of the html string to PDF. Below is the modified code:
private byte[] createPDF(string html, string filename) {
MemoryStream msOutput = new MemoryStream();
string printPDFCSS = Server.MapPath("/content/printPDF.css");
Document doc = new Document(PageSize.LETTER);
doc.SetMargins(doc.LeftMargin, doc.RightMargin, 35, 0);
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msOutput);
doc.Open();
Dictionary<String, String> substFonts = new Dictionary<String, String>();
substFonts["Arial Unicode MS"] = "Helvetica";
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(XMLWorkerHelper.GetCSS(new FileStream(printPDFCSS, FileMode.Open)));
StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(new UnembedFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS, substFonts)));
hpc.SetImageProvider(new ImageProvider(filename));
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParse = new XMLParser(true, worker);
xmlParse.Parse(msOutput);
doc.Close();
return msOutput.ToArray();
}
Bellow is the code I finally found that works, there were a number of issues with the code above, but this seems to work:
private byte[] createPDF(string html, string filename){
MemoryStream msInput = new MemoryStream(ASCIIEncoding.Default.GetBytes(html));
MemoryStream msOutput = new MemoryStream();
string printPDFCSS = Server.MapPath("/content/printPDF.css");
Document doc = new Document(PageSize.LETTER);
doc.SetMargins(doc.LeftMargin, doc.RightMargin, 35, 0);
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msOutput);
doc.Open();
Dictionary<String, String> substFonts = new Dictionary<String, String>();
substFonts["Arial Unicode MS"] = "Helvetica";
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(XMLWorkerHelper.GetCSS(new FileStream(printPDFCSS, FileMode.Open)));
StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(new UnembedFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS, substFonts)));
hpc.SetImageProvider(new ImageProvider(filename));
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParse = new XMLParser(true, worker);
xmlParse.Parse(msInput);
doc.Close();
return msOutput.ToArray();
}

C# 3.0 Save itextsharp pdf to database using MemoryStream

I'm trying to save to databse a pdf file generated by itextsharp. But, I haven't been successfully so far.
I'm using Linq to sql.
Here's the code:
MemoryStream ms = new MemoryStream();
Document d = new Document(PageSize.A4, 60, 60, 40, 40);
PdfWriter w = PdfWriter.GetInstance(d, ms);
w.CloseStream = false;
string txtTemplate = "";
Encoding en = Encoding.GetEncoding("iso-8859-1");
StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("~/Content/templates/CessaoDireitosDica.txt"), en);
txtTemplate = sr.ReadToEnd();
sr.Close();
string conselhos = "";
Font font = new Font(Font.HELVETICA, 11, Font.NORMAL);
font.SetColor(0xC6, 0xC6, 0xC6);
Paragraph txtBody = new Paragraph(txtTemplate, font);
txtBody .SetAlignment(ElementTags.ALIGN_JUSTIFIED);
d.Open();
d.Add(txtBody);
d.Close();
byte[] pdfDone = ms.ToArray();
w.Flush();
ms.Flush();
ms.Close();
return pdfDone;
It throws no error, but it doesn't save nothing in DB. The DB field is an "image" field type.
I also use this code to render a pdf on the fly (I cut off the byte[] pdfDone... and return the MemoryStream).
I don't know what can be wrong... And debugging, I could also see that byte[] pdfDone has a value (something like 3487), but nothing is saved to DB.
Thanks in advance!
function byte[] CreatePdf(){
byte[] result;
using (MemoryStream ms = new MemoryStream())
{
Document pDoc = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(pDoc, ms);
pDoc.Open();
//here you can create your own pdf.
pDoc.Close();
result = ms.GetBuffer();
}
return result;
}

Categories

Resources