Do you have some feature in mind that you'd love to see in Couch? Let us know.
11 posts Page 1 of 2
It would be wonderful if Couch could automatically create a downloadable PDF file of a page - or sequence of pages. Many CMSs - specially wiki-flavoured ones - have this capability. It comes in handy for various applications, such as creating downloadable receipts, or creating customised printable content for members (like certificates or stationery).

There are a couple of powerful open-source php PDF libraries out there, who do not depend on other
libraries being pre-installed or compiled - they work with php "out-of-the-box". They are:

TCPDF - extremely powerful and LGPL Licensed:
http://www.tcpdf.org

FPDF - not as full-featured, but easier to use and has an even more permissive license:
http://www.fpdf.org

The way I imagine it could work, would be through a 'pages'-like tag, which would generate a link to the downloadable pdf file. For instance, to generate a link to download an automatically generated pdf file of the receipt currently being viewed by the user:

Code: Select all
<cms:pdf masterpage='receipt.php' page_name=k_page_title />


An extra parameter could be used to set the page size of the outputted document:

Code: Select all
<cms:pdf masterpage='receipt.php' page_name=k_page_title page_size='A4' />


To output a pdf version of the entire documentation for 'version 1.4.5' of some software, set on A5 size in landscape, we could use:

Code: Select all
<cms:pdf masterpage='doc.php' folder_name='Version 1.4.5" page_size='A5' landscape=1 />


Perhaps this is something that could be considered for Couch version 2.0?,,,
+1 vote from me.

I use mPDF for this .
http://www.mpdf1.com

When mPDF is loaded you can easily create a couch template for creating a pdf.

This couch/mPDF template would look like this:

Code: Select all
<?php require_once( 'couch/cms.php' ); include("mpdf.php");?>

   <cms:template title='your PDF' hidden='1'></cms:template>

     <cms:pages masterpage='couch_page.php'>
   
      <cms:php>

              $html = '
                Here your normal couch code.....
             
              ';


//========mpdf  settings below=======================================
//==============================================================
//==============================================================



$mpdf=new mPDF('c','A4');
$mpdf->SetTitle('Your title');
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;

//==============================================================
//==============================================================
//==============================================================


</cms:php>

      </cms:pages>
<?php COUCH::invoke(); ?>


For sure PDF output by an couch tag would make this much easier.
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
Good suggestion. Shouldn't be difficult to create an addon doing this.
I have some reservations about the following though -
output a pdf version of the entire documentation for 'version 1.4.5' of some software
This, as opposed to single pages, could possibly take a very long time with the risk of the operation timing out - something that end-users always detest.
What do you say? What are your experiences?

With regards to making the PDF of a single page, I find the following quick-n-dirty approach the easiest (works with Chrome) -
1. Create a CSS for print media
2. Display a printer icon on the front-end pages that uses JS to send the page to the printer.
3. Ask the client to choose 'Save as PDF' in the printing dialog box.
KK wrote: Good suggestion. Shouldn't be difficult to create an addon doing this.


Wonderful!!!

KK wrote: I have some reservations about the following though (...)
This, as opposed to single pages, could possibly take a very long time with the risk of the operation timing out - something that end-users always detest.
What do you say? What are your experiences?


I have used some Wikis in the past that were able to output the entire 'book' as a single pdf file. The operation, if I remember correctly, did take several seconds, and the user was informed while that it may take a while.

Outputting a whole book on-the-fly is handy, for sure, but it is a very specific use-case. It is particularly useful for documentation and manuals that are constantly being updated.

The "quick-and-dirty" approach of creating a PDF from the print-dialog box is fine, if the user is savvy, but being able to have a clickable link would be truly awesome! :-)
Tomarnst wrote: +1 vote from me.

I use mPDF for this .
http://www.mpdf1.com

When mPDF is loaded you can easily create a couch template for creating a pdf.

This couch/mPDF template would look like this:

Code: Select all
<?php require_once( 'couch/cms.php' ); include("mpdf.php");?>

   <cms:template title='your PDF' hidden='1'></cms:template>

     <cms:pages masterpage='couch_page.php'>
   
      <cms:php>

              $html = '
                Here your normal couch code.....
             
              ';


//========mpdf  settings below=======================================
//==============================================================
//==============================================================



$mpdf=new mPDF('c','A4');
$mpdf->SetTitle('Your title');
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;

//==============================================================
//==============================================================
//==============================================================


</cms:php>

      </cms:pages>
<?php COUCH::invoke(); ?>


For sure PDF output by an couch tag would make this much easier.


I did try this code now, but i just get CAN NOT FIND PAGE. Must i have some special code between the

$html = '
Here your normal couch code.....

';
Hi,

I suppose couch can't find your template

<cms:template title='your PDF' hidden='1'></cms:template>
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
Tomarnst wrote: Hi,

I suppose couch can't find your template

<cms:template title='your PDF' hidden='1'></cms:template>


Is it possible to explain abit more on how to implement this.

If i have a page view ks.php showing some info.

On this view page i wanna have the PDF link.

Must i put the code you show here in my view page, or must i make a new couch page and put code there*?

Hope you can reply on this. Realt need PDF function :)
hi,

Must i put the code you show here in my view page, or must i make a new couch page and put code there*?


Did you download mpdf ? http://www.mpdf1.com

When downloaded you could make a couch template like posted before.

change
Code: Select all
include("mpdf.php");
to the path of your mpdf file.

Now this couch template will generate a pdf , you can use your normal couch code in the html variable

Code: Select all
$html = '
                Here your normal couch code.....
              ';
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
Do we have a couch tag/plugin for this? If not i would vote for this too. Would be a great addon for our cms. ;)
Hello @Tomarnst, @luxlogica, @kimheggen, @KK Sir,

I am trying to generate pdf using the mpdf (https://github.com/mpdf/mpdf.github.io).

I have a list of students (clonable student.php), I need to generate Bonafide Certificate of the students. I took to AJAX to do so. But the issue is that in the XHR Response Payload I get a random string, which when manually appended to "data:application/pdf;base64,<string>" displays the pdf.

I am not able to open in new tab or download the pdf.

My codes are as follows:

Student List:
Code: Select all
<?php 
   require_once '../couch/cms.php';
   require_once __DIR__ . '/assets/vendor/autoload.php';
?>
<cms:template title="PDF Generation" clonable="1" parent="_pdf_generation_" order="1" >
   <cms:editable name="fname" label="First Name" type="text" order="1" />
   <cms:editable name="lname" label="Last Name" type="text" order="2" />
   <cms:editable name="dob" label="Date of Birth" type="datetime" order="3" default_time="@current" />
</cms:template>
<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="author" content="GenXCoders Pvt Ltd (GXCPL)">
      <title>PDF Generation</title>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" integrity="sha512-pli9aKq758PMdsqjNA+Au4CJ7ZatLCCXinnlSfv023z4xmzl8s+Jbj2qNR7RI8DsxFp5e8OvbYGDACzKntZE9w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" integrity="sha512-5A8nwdMOWrSz20fDsjczgUidUBR8liPYU+WymTZP1lmY9G6Oc7HlZv156XqnsgNUzTyMefFTcsFH/tnJE/+xBg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
      <style type="text/css">
         body {
            font-family: FontAwesome, 'Helvetica Neue', Helvetica, Arial, sans-serif;
         }
      </style>
   </head>
   <body>

      <div class="container">
         <div class="row">
            <div class="col-md-12">
               <h1>PDF Generation</h1>
            </div>
            <div class="col-md-12">
               <div class="table-responsive">
               <table class="table table-striped">
                  <thead>
                     <tr>
                        <th>#</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Date of Birth</th>
                        <th class="text-center">Action</th>
                     </tr>
                  </thead>
                  <tbody>
                     <cms:pages masterpage=k_template_name>
                     <cms:no_results>
                     <tr>
                        <td class="text-center" colspan="5">
                           - No record(s) found -
                        </td>
                     </tr>
                     </cms:no_results>
                     <tr>
                        <td>
                           <cms:show k_absolute_count />
                        </td>
                        <td>
                           <cms:show fname />
                        </td>
                        <td>
                           <cms:show lname />
                        </td>
                        <td>
                           <cms:date dob format='d-m-Y' />
                        </td>
                        <td class="text-center">
                           <button class="btn btn-danger btn-sm text-uppercase" type="button" data-id="<cms:show k_page_id />">
                              <i class="fa fa-file-pdf-o"></i> Generate PDF
                           </button>
                        </td>
                     </tr>
                     </cms:pages>
                  </tbody>
               </table>
               </div>
            </div>
         </div>
      </div>

      <script src="https://code.jquery.com/jquery-3.0.0.js" integrity="sha256-jrPLZ+8vDxt2FnE1zvZXCkCcebI/C8Dt5xyaQBjxQIo=" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js" integrity="sha512-hQeArtKSBTJ1segRjyRoDh409td70Loy62usBB3+Ox5Z1akisBzCoZWpU9+Q/N5GBbgUJ6Xo1fcdFgDDIRzLxA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
      <script type="text/javascript">
         $("button").click(function(e) {
             e.preventDefault();
             var i = $(e.currentTarget).attr('data-id');
             $.ajax({
                 type: "POST",
                 url: "<cms:show k_site_link />pdf-generation/pdf-generation-ajax.php",
                 data: {
                     id: i
                 }
             });
         });
      </script>
   </body>
</html>
<?php COUCH::invoke(); ?>


PDF Generator:
Code: Select all
<?php 
   require_once '../couch/cms.php';
   require_once __DIR__ . '/assets/vendor/autoload.php';
?>
<cms:template title="AJAX - PDF Generation" parent="_pdf_generation_" order="2" />
<cms:set page_id="<cms:gpc 'id' method='POST' />" scope="global" />
<cms:content_type 'application/pdf'/>

<cms:pages masterpage="pdf-generation/index.php" id=page_id limit="1">
   <cms:php>      
      $html = '
         <div>
            First Name: <cms:show fname />
         </div>
         <div>
            Last Name: <cms:show lname />
         </div>
         <div>
            Date of Birth: <cms:date dob format='d-m-Y' />
         </div>
      ';
   
      $mpdf = new \Mpdf\Mpdf(['format' => 'A4-L']);
      $mpdf->SetDisplayMode('fullpage');
      $mpdf->WriteHTML($html);
      $mpdf->Output();

   </cms:php>
</cms:pages>
<?php COUCH::invoke(); ?>


AJAX Script:
Code: Select all
$("button").click(function(e) {
    e.preventDefault();
    var i = $(e.currentTarget).attr('data-id');
    $.ajax({
        type: "POST",
        url: "<cms:show k_site_link />pdf-generation/pdf-generation-ajax.php",
        data: {
            id: i
        }
    });
});


I tried the parameters:
I - Open in new tab
D - Download PDF to local storage
in $mpdf->Output(); with the file name set explicitly. But nothing happens.
F - Save to folder works fine, but that will need server access to download or view the files at the client end, which will not be a good option.

There are 13000+ records to be processed for generating Bonafide Certificates, Transfer Certificates, Other Documents.

I know this is not exactly a CouchCMS issue, but those who have worked on it, if they could shed some light, I would be really grateful.

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
11 posts Page 1 of 2