0

I've read PDF Editing in PHP? but haven't found quite what I'm looking for.

Basically I need to take a DOC (read: not .docx) file, which will be used as a template, find certain strings (e.g, [Patient Number], [Site Number], etc) replace them with values that I'll retrieve from a database and then save that file as a .pdf file.

Here's the important points

  • I'd rather avoid zend framework (or any framework) for the purpose of keeping my application's software light and not having to maintain another 3rd party piece of code in this project (jquery,jquery-UI,jqGrid, etc)
  • I can't use DOCX because I don't have that version of Office installed on my work computer. I will eventually, but I don't know when that'll happen and I need to finish coding this ASAP
  • The converting to PDF at the end is somewhat optional. I'd like to have it as interoperable as possible. I already have ezPdf and fPdf on the system, so perhaps I can just feed the generated Word binary into ezPdf/fPdf and generate the PDF that way.

Are there any PHP libraries out there that allow reading a .doc file and then the ability to edit the text and then write it back to the disk?

I've tried ezPdf and fPdf, but the specifying of text block coordinates got too tedious and involved too much guesswork and trial & error.

Thanks in advanced!

Community
  • 1
  • 1
  • Do you want to preserve formatting? – wanovak Jul 08 '11 at 15:58
  • Yup. Needs to look the exact same. The source file is basically a form. My code has to take the file and fill in as much information we have available on a particular patient, and then have an employee print it out to mail it off to the study site so the physician can fill in the missing data and send it back to us. So, yes, it needs to be a 1:1 carbon copy (except for the places where I fill in text) – WhiskeyTangoFoxtrot Jul 08 '11 at 16:00

3 Answers3

1

If it is a standardized form AND the text does not need to reflow based upon what is entered (e.g. you are using fixed boxes and/lines, rather than inserting sentences into a paragraph), you might consider the straightforward idea of treating the form as an image and then writing text to it at fixed coordinates either using a PDF api (acrobat pdf supports forms) or emitting the resulting as a pdf file.

horatio
  • 1,426
  • 8
  • 7
  • It is a standardized form and we're only entering in a small amount of text in each block (name or a number vs lines and paragraphs). However I find working with coordinates to be too frustrating. I tried this approach before on an unrelated project and it resulted in hours of experimentation to get just the right position. That's why I'm opting to edit the text directly rather than stamp it on using coordinates. – WhiskeyTangoFoxtrot Jul 08 '11 at 20:31
  • 1
    http://stackoverflow.com/questions/77873/filling-pdf-forms-with-php discusses the PDF form support with pdftoolkit. You could use PDF forms instead of DOC format. (I sort of touched on it, but not well) – horatio Jul 08 '11 at 20:35
0

Reading/Writing a MS Word file in PHP

Community
  • 1
  • 1
Mike
  • 23,542
  • 14
  • 76
  • 87
0

I ended up using docx (with the assistance of my co-worker (who had office 2007) exporting the file for me) and completely (for now at least) forsaking the PDF conversion.

I did install phpdocx and after juggling around with a bunch of extensions (XSL, mbstring, etc) I had to get the server administrator to install, it did generate the PDF based off of my generated docx file, it didn't convert the styles or image used in the header, so it looked absolutely horrible.

Due to time constraints, I had to completely skip out on PDF conversion and just use the docx file. This will be something we re-visit in the future, but for now, docx it is.

We might also look into LiveDocX, but unfortunately that deals with uploading files to the cloud and because these files contain patient date (however, mostly blinded) I have to get approval from our legal department to make sure we're in accordance with HIPAA privacy laws.