CKEditor4 Plugin Code for Api2Pdf REST API
Api2Pdf.com is a REST API for instantly generating PDF documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), and images. The API also supports merge / concatenation of two or more PDFs. Api2Pdf is a wrapper for popular libraries such as wkhtmltopdf, Headless Chrome, and LibreOffice.
This plugin adds a Save to PDF functionality to CKEditor4. It will take the HTML contents of your editor, convert it to PDF, and request the web browser to download it.
The plugin will add an icon to the toolbar to Save to PDF.
- See Demo
- Get Started with CKEditor4 CDN
- Handlers and API Key
- PHP Handler
- ASP.NET (.NET Core, MVC, Classic ASHX)
- FAQ
This sample HTML initializes CKEditor4 via CDN and loads the latest Save-to-Pdf plugin via CDN. This example HTML replaces all textareas in a document to use CKEditor4 loaded via CDN. Take a look at the init method, to make use of the save to pdf plugin you should take note of the following steps:
- Register save-to-pdf with addExternal
- Register save-to-pdf in the extraPlugins section
- Finally you must set the handler. For more information on the handler see this section
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.ckeditor.com/4.10.0/standard/ckeditor.js"></script>
<script>
CKEDITOR.plugins.addExternal( 'save-to-pdf', 'https://rawgit.com/Api2Pdf/api2pdf.ckeditor4/master/plugins/save-to-pdf/', 'plugin.js' );
</script>
</head>
<body>
<textarea name="editor1"></textarea>
<script>
CKEDITOR.replace( 'editor1',
{
extraPlugins: 'save-to-pdf',
pdfHandler: 'REPLACE-WITH-HANDLER-URL'
} );
</script>
</body>
</html>
Whether you are using the CDN or self-hosting, you will need a server side handler to process the PDF generation request. This handler will take your incoming HTML and send it to Api2Pdf.com for processing.
- Create an account at portal.api2pdf.com to get your API key.
- Use the approproiate code example for your platform of choice PHP Handler or ASP.NET
- Place your Api2Pdf API key in the handler code
- Update the saveToPdfHandler configuration in your ckeditor replace function
-
Download the PHP Source code here: https://github.com/Api2Pdf/api2pdf.ckeditor4/tree/master/handlers/php api2pdf.php is the library code copied from https://github.com/Api2Pdf/api2pdf.php index.html assumes the sample was copied to /php/savetopdf.php
-
In savetopdf.php update the API key with the one you created at api2pdf.com
$a2p_client = new Api2PdfLibrary('YOURAPIKEY');
- Update the saveToPdfHandler configuration in your ckeditor replace function. See example
-
The ASP.NET sample code makes use of the Nuget Package API2PDF. Assuming you are building your own package you will first want to Install-Package Api2Pdf (https://github.com/Api2Pdf/api2pdf.dotnet)
-
Copy the appropriate handler code
- Regardless of which handler you copy, you will want to update this line of code with the API key you created at api2pdf.com
var a2pClient = new Api2Pdf("YOURAPIKEY");
- Update the saveToPdfHandler configuration in your ckeditor replace function See example