Welcome Guest Search | Active Topics | Sign In | Register

How to generate a PDF with 2 pages,one with ShrinkToFit and one Without Options
SK
Posted: Monday, March 21, 2016 2:36:21 PM
Rank: Newbie
Groups: Member

Joined: 3/17/2016
Posts: 9
I am using the latest trial version of EO PDF to print an MVC view which calls 2 other views using Html.RenderAction method. Everything works fine. But now what i want to do is print the response from first RenderAction in one page with ShrinkToFit option and the second one with normal settings (not shrinking).

Here is how my view looks like
<h2>Main page</h2>
@{ Html.RenderAction("Page1","MycontrollerName"); }
@{ Html.RenderAction("Page2","MycontrollerName"); }

I want to print the response of Page1 action metod in one page ( shrinked to fit) and Page2 to be normal ( goes to a third page if the content is longer than a normal page). Is this possible? I have MVCToPDF.RenderAsPDF(); in my action method to render the action method as PDF

My assumption was , i should build 2 pdfs, one with shrink to fit and one without that and merge it. Is that right ? Is there some sample code which does this ? I prefer to not store the indidual pdfs(for page 1 and page 2) in the disk , but in the memory.

Thanks
eo_support
Posted: Monday, March 21, 2016 4:10:43 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

You can render the two views into strings and then do something like this:

Code: C#
//Create the first document with ShrinkToFit on X
PdfDocument doc1 = new PdfDocument();
HtmlToPdf.Options.AutoFitX = HtmlToPdfAutoFitMode.ShrinkToFit;

//It's important that you must set BaseUrl correctly in order for
//embedded resources to be resolved properly
HtmlToPdf.Options.BaseUrl = your_base_url;

//Render the result into doc1
HtmlToPdf.ConvertHtml(html_for_view1, doc1);

//Create the second document without ShrinkToFit
PdfDocument doc2 = new PdfDocument();
HtmlToPdf.Options.AutoFitX = HtmlToPdfAutoFitMode.None;
HtmlToPdf.Options.BaseUrl = your_base_url;
HtmlToPdf.ConvertHtml(html_for_view2, doc2);

//Merge the two documents
PdfDocument result = PdfDocument.Merge(doc1, doc2);

//Save the result into a byte array
MemoryStream ms = new MemoryStream();
result.Save(ms);
byte[] pdfData = ms.ToArray();


Hope this helps. Please feel free to let us know if you still have any questions.

Thanks!
SK
Posted: Tuesday, March 22, 2016 10:52:05 AM
Rank: Newbie
Groups: Member

Joined: 3/17/2016
Posts: 9
Thanks ! Is it possible to use HtmlTpPdf.ConvertUrl instead of ConvertHtml ?
eo_support
Posted: Tuesday, March 22, 2016 10:54:07 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

You can use either ConvertHtml or ConvertUrl. The above code is just an example to show you how do two separate conversions with different parameters and then merge the result into a single PDF file.

Thanks!
SK
Posted: Tuesday, March 22, 2016 11:04:39 AM
Rank: Newbie
Groups: Member

Joined: 3/17/2016
Posts: 9
Thanks ! Once i merge, How will i return the PdfDocument to user ? Just calling MVCToPDF.RenderAsPDF() is good enough ? In that case, why would i need the memorystream ?
eo_support
Posted: Tuesday, March 22, 2016 11:39:14 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

You can't use MVCToPDF.RenderAsPDF for that purpose, that method does a whole lot more than sending the files to the client. Sending dynamic contents down to the client is a very generic ASP.NET programming question and people do that all the time (for example, a page to serve dynamically generated images). So we consider that out of the scope of our support but if you search online you should be able to find plenty of sample code to do that. Basically you need to write to Response.OutputStream directly.

You do not necessary need MemoryStream. That's just sample code to show you that you can get all the PDF file contents without having to create a physical file.

Thanks!
SK
Posted: Tuesday, March 22, 2016 11:40:43 AM
Rank: Newbie
Groups: Member

Joined: 3/17/2016
Posts: 9
Excellent ! Thanks. I like EO.Pdf. :) Cheers :)


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.