# Export PDF file from an Editor and Load it into a PDFViewer

## Environment

<table>
	<tbody>
		<tr>
			<td>Product Version</td>
			<td>2022.3.913</td>
		</tr>
		<tr>
			<td>Product</td>
			<td>Editor for Progress® Telerik® UI for ASP.NET MVC</td>
		</tr>
        <tr>
			<td>Product</td>
			<td>PDFViewer for Progress® Telerik® UI for ASP.NET MVC</td>
		</tr>
	</tbody>
</table>

## Description

How can I export a PDF file with a page template from a hidden Editor and load it into a PDFViewer?

## Solution

The example below relies on the following key steps:

1. Create a hidden [Editor with specified PDF export settings](https://demos.telerik.com/aspnet-mvc/editor/pdf-export), a [page template](https://docs.telerik.com/kendo-ui/framework/drawing/pdf-output/page-templates), and a [PDFViewer](https://demos.telerik.com/aspnet-mvc/pdfviewer).
2. Override the default implementation of the [`saveAsPDF()`](https://docs.telerik.com/kendo-ui/api/ui/editor/methods/saveaspdf) method of the Editor so the data about the PDF file is accessible in the returned promise in the PdfExport event handler.
3. Handle the [`PdfExport event`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/editoreventbuilder#pdfexportsystemfunc) of the editor and pass the file to the PDFViewer by using the [`fromFile()`](https://docs.telerik.com/kendo-ui/api/ui/pdfviewer/methods/fromfile) method.
4. When the page is loaded, get a reference to the hidden Editor, set the page template in the Editor's PDF options, and trigger its `saveAsPDF()` method.

   ```Razor Index.cshtml
       @(Html.Kendo().PDFViewer()
           .Name("pdfviewer")
       )

       <div class="editor-block" style="visibility: hidden;">
           @(Html.Kendo().Editor()
               .Name("editor")
               .Pdf(pdf => pdf
                   .Margin(20, 20, 20, 20)
                   .PaperSize("A4")
               )
               .Tools(tools => tools
                   .Pdf()
               )
               .Events(events => events.PdfExport("onPdfExport"))
               .Value(@<text>Editor content</text>)
           )   
       </div>
   ```

   ```JavaScript
       <script>
           // Ensure that the script below is included before the initialization of the Editor.
           kendo.ui.Editor.fn.saveAsPDF = function() {
               var progress = new $.Deferred();
               var promise = progress.promise();
               var args = { promise: promise };

               if (this.trigger("pdfExport", args)) {
               return;
               }

               var options = this.options.pdf;

               this._drawPDF(progress)
               .then(function(root) {
               return kendo.drawing.exportPDF(root, options);
               })
               .done(function(dataURI) {
               progress.resolve(dataURI);
               })
               .fail(function(err) {
               progress.reject(err);
               });
               return promise;
           };

           function onPdfExport(e) {
               e.promise.then(function(data) {
                   var viewer = $("#pdfviewer").data("kendoPDFViewer");
                   viewer.fromFile({ data: data.split(',')[1] });
               });
           }

           $(document).ready(function () {
               var editorComponent = $("#editor").getKendoEditor();
               editorComponent.options.pdf.template = $("#page-template").html();
               editorComponent.saveAsPDF();
           });
       </script>

   ```

   ```JS PDF_PageTemplate
       <script type="x/kendo-template" id="page-template">
           <div class="page-template" style="position:absolute; top:0; left:0; width:100%; height:100%">
               <div class="header" style="position:absolute; top:10px; left:20px; width:100%; font-size:18px; border-bottom: 1px solid black;">
                   Header Text
               </div>
               <div class="footer" style="position:absolute; bottom:20px; right:10px; width:100%;">
                   <div style="float: right">Page #: pageNum # of #: totalPages #</div>
               </div>
           </div>
       </script>
   ```

For a runnable example based on the code above, refer to the [REPL example on exporting a PDF file from an Editor](https://netcorerepl.telerik.com/wmFaGhGv31uIA5d258).

## More ASP.NET MVC Editor Resources

* [ASP.NET MVC Editor Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/editor/overview)

* [ASP.NET MVC Editor Demos](https://demos.telerik.com/aspnet-mvc/editor)

* [ASP.NET MVC Editor Product Page](https://www.telerik.com/aspnet-mvc/editor)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Telerik REPL: Exporting a PDF File from an Editor and Loading It into a PDFViewer](https://netcorerepl.telerik.com/wmFaGhGv31uIA5d258)
* [Client-Side API Reference of the Editor for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/editor)
* [Client-Side API Reference of the PDFViewer for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/pdfviewer)
* [Server-Side API Reference of the Editor for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/editor)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
