Forum Moderators: coopster

Message Too Old, No Replies

Creating pdfs with php

         

icemancast

8:01 am on Jan 6, 2008 (gmt 0)

10+ Year Member



I am creating a pdf file with php. the file gets created but it is corrupt and i can't figure why. here is what i ultimately want to do to open an existing pdf add a bit of text and save it to the server. really here is my question. what is best way to go about distributing pdf file to people who purchase it, and at same time protect it from access of the server? below is code that is messing up.

<?php
$pdf = PDF_new();
pdf_open_file($pdf, "/hsphere/local/home/cefalu/example.com/pdfs/hello.pdf");
/* open new PDF file; insert a file name to create the PDF on disk */
if (PDF_begin_document($pdf, "", "") == 0) {
die("Error: " . PDF_get_errmsg($pdf));
}

pdf_set_info($pdf, "Creator", "hello.php");
pdf_set_info($pdf, "Author", "Rainer Schaaf");
pdf_set_info($pdf, "Title", "Hello world (PHP)!");

pdf_begin_page($pdf, 595, 842, "");

$font = PDF_load_font($pdf, "Helvetica-Bold", "winansi", "");

PDF_setfont($pdf, $font, 24.0);
PDF_set_text_pos($pdf, 50, 700);
PDF_show($pdf, "Hello world!");
PDF_continue_text($pdf, "(says PHP)");
pdf_end_page($pdf);
pdf_close($pdf);
pdf_delete($pdf);

$len = filesize("/hsphere/local/home/cefalu/example.com/pdfs/hello.pdf");

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
readfile("/hsphere/local/home/cefalu/example.com/pdfs/hello.pdf");

?>

[edited by: jatar_k at 2:02 pm (utc) on Jan. 6, 2008]
[edit reason] please use example.com [/edit]

coopster

2:21 pm on Jan 7, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Something to do with the deprecated function(s) you are using, perhaps? I'm not entirely certain as I don't use the PDF Functions [php.net] (they require you purchase a license for commercial use). But a quick read tells me that you may want to review that page to determine which functions you are using that are deprecated. There is an example on that page that may be helpful as well.

whoisgregg

6:48 pm on Jan 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you are modifying the filesize of the original hello.pdf file by adding additional content, the value in the $len variable is not going to match the actual filesize of the downloaded file. When you provide a Content-Length: value, the browser expects it to be exact.

To get the content length of the PDF file in memory, I believe you use a combination of PDF_get_buffer and strlen on that buffer. I'm not 100% sure though, it's just something I read somewhere.