3

I'd like to be able to output a pdf in browser (which is all fine and dandy) but limit the number of pages shown. Ie. the reader can see the content of what's he's trying to buy is accurate but at the same time I`m not handing the file for free. I can batch edit the files to create new 3-5 paged pdf's for my purpose, but that's a hassle and I'd also have to upload the 'landing page' pdf's to the server, keeping in mind the category of the product and whatnot.

A simplified version of what I'm asking,

user -> download link -> pdf opens with the first 5 pages -> adds a 'buy now' on the 6th pdf page;

I am happy to do this through a third-party script. The only thing required would be for the user to not be able to access the entire pdf doc (like if the trigger-link would be <a href="link.tld/name" id="trigger"> the user shouldn`t be able to fetch the url of the full-doc.

PJ Bergeron
  • 2,788
  • 4
  • 25
  • 42

4 Answers4

1

The only possible way I think is to create another copy of PDF and it will have only first five pages only...and then from 6th page the message will be there to Buy this Book ans How to Buy and what is amount etc.....

So , just create another preview like version

MONTS_MIND_Hacker
  • 1,727
  • 2
  • 9
  • 9
  • Yeah, bulk edit it is then. Wanted to see if there are any viable options, if the idea is viable (at all) and of course, risk-free environment. Wanted to check with the prost of the field first. Thanks for your interest/time! – Iulia Bancescu Mar 10 '15 at 03:25
  • as of now I had given very simple solution but it will not be feasible when there is 10000+ files.....So , I will try my best to find best programmatic solution.. – MONTS_MIND_Hacker Mar 10 '15 at 03:30
  • Yeah, I know what you mean. I have in plan to also open up in the literature ebooks department and there there would be a ton of files. Take a look at what @user2768665 said. I'm going to write the full rundown now and maybe see if we can improve this for other people. Thank you ! – Iulia Bancescu Mar 10 '15 at 03:47
  • ya , right.....that user has given OK solution...Finally , it solved....Keep asking question...thnx – MONTS_MIND_Hacker Mar 10 '15 at 08:15
1

You can actually do it by adding another copy of the pdf file on your webserver and using tcpdf php class, you can actually restrict pages, by deleting remaining pages.

require_once('tcpdf_include.php');

$pdf->deletePage(6,100);//Deletes pages from 6-100

You can have a good read about the complete tcpdf here

This is my choice for any pdf handlers on my site.

Gururaju Hiddenx
  • 199
  • 1
  • 4
  • 19
  • Great answer, I've finished reading the example. This is indeed perfect for what I'm trying to do. I`m going to mark your answer right and write another answer as to how I`m going to implement this, maybe it'll help others with the same type of specific issue! Thanks a lot! – Iulia Bancescu Mar 10 '15 at 03:45
0

Thanks again to @user2768665 for the best answer to my troubles. This is my gameplan:

Since I have the files named as in the link, I first grab the WP link of the product using GET, then store that in a variable, preg_replace it to erase the rest of the link (with regex), store that result in a new var, let's say $link2p .

Then I add the call to the product (Download) to open up the processing php script, use tcpdf (user2768665's answer) to delete anything that goes after page 5, add the page with the company's logo as being page 6, and at the end of it add the

Click <a href="http://domain.ltd/saleProcessPage/<?php echo $link2p;?>"here</a> to download <?php echo $link2p;?> FULL PDF

Or, in my case (since I have the a 3rd party paying platform -and you might have the same-), we first run a database check on the product(based on its name) to retrieve the link to our product in our paying platform and then we do the same as above.

Then, we modify the the original's pdf name and output it differently;

$pdf->Output($link2p.'.pdf', 'Insert w/e random string generator.pdf');

It's a great solution as it keeps original pdf files out of any "Inspect Element" user, it runs in the backend and hey, random name si given out.

// Other things I`m thinking about:

Since I haven't read the full documentation for tcpdf, can't be sure on this but I haven't seen any page count ability (which is needed in order to do this fully automatically for the files: i.e. $pdf->deletePage(6,100); becomes $pdf->deletePage(6,endofpdf); For that purpose, I add what I've read before placing the question : Count the number of pages in a PDF in only PHP . So we store that into a $count variable (to see how many files the pdf has) so that we can use $pdf->deletePage(6,$count); Again, haven't read the tcpdf full documentation, so this may be useless.

I'm going to do a full code workup on this after I get some sleep and post the example in its final form. Hopefully this will help others stuck in the same place.

A great thank you again to @user2768665 and @Monts_mind_hacker for the interest in the topic. Awesome guys.

Community
  • 1
  • 1
0

You can use FPDI to create a preview of the first 5 pages:

$pdf = new FPDI();
$pageCount = $pdf->setSoruceFile('your.pdf');
$pageCount = min($pageCount, 5);

for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
     $tplIdx = $pdf->importPage($pageNo);
     $s = $this->getTemplatesize($tplIdx);
     $this->AddPage($s['w'] > $s['h'] ? 'L' : 'P', array($s['w'], $s['h']));
     $this->useTemplate($tplIdx);
}

$pdf->AddPage();
// add the link to the shopping cart on the last page...

$pdf->Output('5-pages.pdf', 'D');
Jan Slabon
  • 4,736
  • 2
  • 14
  • 29