Create PDF in Memory Instead of Physical File

Create PDF in memory instead of physical file

Switch the filestream with a memorystream.

MemoryStream memStream = new MemoryStream();
PdfWriter wri = PdfWriter.GetInstance(doc, memStream);
...
return memStream.ToArray();

How to create pdf in memory and not physically with ghostscript?

You can't do it in memory with Ghostscript, the PDF file is always written to a file system. You can store it on Ghostscript's RAM file system, but that's unlikely to help you with what you are trying to do.

Don't use -dUseCIEColor.

You aren't running a pdfa_def.ps file, which is required to set up some aspects of a PDF/A output file. See the documentation for more details.

iText 7 - HTML to PDF write to MemoryStream instead of file

You meddle with the workStream before the document and pdfWriter have finished creating the result in it. Furthermore, the intent of your meddling is unclear, first you retrieve the bytes from the memory stream, then you write them back into it...?

public FileStreamResult pdf()
{
var workStream = new MemoryStream())

using (var pdfWriter = new PdfWriter(workStream))
{
pdfWriter.SetCloseStream(false);
using (var document = HtmlConverter.ConvertToDocument(html, pdfWriter))
{
}
}

workStream.Position = 0;
return new FileStreamResult(workStream, "application/pdf");
}

By the way, as you are essentially doing nothing special with the document returned by HtmlConverter.ConvertToDocument, you probably could use a different HtmlConverter method with less overhead in your code.

How to create pdf files in memory

If I look at the subject of your question, then the answer is simple. You are creating a PDF on the file system using a FileOutputStream. In the subject you ask: "How to create pdf files in memory?" That is simple. iText accepts any OutputStream, so the answer is: use a ByteArrayOutputStream instead of a FileOutputStream.

See Using iText, generate on memory a PDF that is generated on disk instead

Document document = new Document();
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(documento, out);
(...)
byte[] pdf = out.getBytes();

However, why would you want to create a PDF in memory on an Android device? What are you going to do with those bytes? You can't show the PDF if you only have a byte[]. That is explained in my answer to this question: Display PDF document stored in a ByteArrayOutputStream (not in a file)

You will also notice that the following question remained unanswered (because there is no answer): How to visualize a ByteArrayOutputStream as PDF on Android?

Your question changes subject towards the end, where you write: "The application can run normally, but can't create pdf file and I can't find." This sounds as if you don't know how to write a PDF to the file system on Android. That's a different question than what you mention in the subject. That question has been answered many times before:

  • Creating PDFs in android
  • how to solve read only file system error in android programming with eclipse
  • How to save PDF file on Sdcard created using iText?
  • Not able to run simple program of iText library in android
  • Save iText created PDF to internal memory as MODE_WORLD_READABLE

Create a document in memory?

Here's a code example that looks like it should work... (havent tried it myself)

http://www.nabble.com/Re%3A-Create-Pdfreader-from-Memory-Stream-p22792260.html

iText 7 and C# writing a PDF file from MemoryStream?

Try this

I dont have IDE for test, but i think this work

MemoryStream baos = new MemoryStream();
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdfDocument = new PdfDocument(writer.SetSmartMode(true));

Document d = new Document(pdfDocument, iText.Kernel.Geom.PageSize.LETTER);
d.Add(new Paragraph("Hello world!"));

d.Close();

byte[] byte1 = baos.ToArray();
File(byte1, "application/pdf", "C:\\iTextTester\\test.pdf");

iTextSharp + FileStream = Corrupt PDF file

I think your problem was that you weren't properly adding content to your PDF. This is done through the Document.Add() method and you finish up by calling Document.Close().

When you call Document.Close() however, your MemoryStream also closes so you won't be able to write it to your FileStream as you have. You can get around this by storing the content of your MemoryStream to a byte array.

The following code snippet works for me:

using (MemoryStream myMemoryStream = new MemoryStream()) {
Document myDocument = new Document();
PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);

myDocument.Open();

// Add to content to your PDF here...
myDocument.Add(new Paragraph("I hope this works for you."));

// We're done adding stuff to our PDF.
myDocument.Close();

byte[] content = myMemoryStream.ToArray();

// Write out PDF from memory stream.
using (FileStream fs = File.Create("aTestFile.pdf")) {
fs.Write(content, 0, (int)content.Length);
}
}

iTextSharp - Sending in-memory pdf in an email attachment

Have you tried:

PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);

// Build pdf code...

writer.CloseStream = false;
doc.Close();

// Build email

memoryStream.Position = 0;
mm.Attachments.Add(new Attachment(memoryStream, "test.pdf"));

If my memory serves me correctly, this solved a similar problem in a previous project.

See http://forums.asp.net/t/1093198.aspx



Related Topics



Leave a reply



Submit