PHP not auto-populating data in PDF - php

I've been using the advice given in this question:
Merge FDF data into a PDF file using PHP
to determine how I can pre-populate a pdf form with data pulled from a database but not getting the data to file. I'm running php 5.3.15 on Mountain Lion and I'm using the following link to combine the pdf with the fdf on a table page that I'd like to allow the user to click and take them to a populated pdf file:
something.pdf#FDF=generatePdf.php
The generatePdf.php looks like this:
<?php
$pdf_form_url= "";
require_once('forge_fdf.php');
$fdf_data_names= array(); // none of these in this example
$fdf_data_strings= array(); // none of these in this example
$fdf_data_strings['form1[0].#subform[0].q1a_LastName'] = "Smith";
$fields_hidden= array();
$fields_readonly= array();
// set this to retry the previous state
$retry_b= false;
header( 'content-type: application/vnd.fdf' );
echo forge_fdf( $pdf_form_url,
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly );
?>
and the FDF file looks like this when I run it through the pdftk command line:
FieldType: Text
FieldName: form1[0].#subform[0].q1a_LastName[0]
FieldNameAlt: Enter Family Name
FieldFlags: 0
FieldJustification: Left
FieldMaxLength: 35
Still, when I run it the form doesn't auto populate. I've tried many variations including just using the q1a_LastName by itself and I still end up with an empty form. The forge_fdf.php is in the same directory.
Update:
I've also attempted the conventional method outlined in the Merge FDF data into a PDF question that I've linked to using PHP, meaning I didn't just direct the user to a pdf file but also attmepted to have the user download the pdf. In that particular case, I would receive a message saying the pdf was empty. I've checked the created raw FDF file and it appears to be populating with data. In either case, I'm not getting what I need.

After much trial and error I realized that the best way to autopopulate to a url linking to a pdf is to define the $pdf_form_url variable for the code above. This code will echo out the details of the fdf file. If you explicitly specify what the pdf url is, php and your browser will take the necessary steps to link you to a the pdf in your web browser with the embedded field information coming from the fdf output.

So I stumbled upon this thread and couldn't get it to work even when I populate the $pdf_form_url variable. Here's my code:
require_once('forge_fdf.php');
// leave this blank if we're associating the FDF w/ the PDF via URL
$pdf_form_url= "http://test.pdf";
// $pdf_form_url= "";
// default data; these two arrays must ultimately list all of the fields
// you desire to alter, even if you just want to set the 'hidden' flag;
//
//
$fdf_data_names= array(); // none of these in this example
$fdf_data_strings= array(); // none of these in this example
$fdf_data_strings['Address, Row 1']="XXXX Balboa Arms Drive #XXX, San Diego CA 01234";
$fields_hidden= array();
$fields_readonly= array();
// set this to retry the previous state
$retry_b= false;
header( 'content-type: application/vnd.fdf' );
echo forge_fdf( $pdf_form_url,
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly );
And here's the form field I want to update using pdftk dump_data_fields:
FieldType: Text
FieldName: Address, Row 1
FieldNameAlt: (Address, <Row 1>)
FieldFlags: 0
FieldJustification: Center

Related

Use qrencode to generate png/svg QR code for Wireguard client configuration

I am looking to generate a QR code on a PHP webpage. The user starts on a page where they submit a form, the only input being the name of the new client. This page is a manager for VPN clients.
After the redirect, PHP generates the configuration file on the page to copy and paste. The user must save this file as wg.conf in their etc directory. I have been trying to use qrencode, a Linux based command line tool to generate the QR code on that same page with the configuration file. According to the documentation on qrencode, you can save the file as a .png or .svg.
I have been playing around with it, both trying to pass data through the URL as GET parameters in the redirect (the only issue is the QR code is sensitive) and by using the backtick operators to run bash commands to save it as a file in the user's local files. I can't seem to figure out where to save it as a file or whether there's another solution to display it on the webpage.
how i would do it:
check this library https://github.com/neocotic/qrious , all you need to do it load the js on your page
generate text you want to convert to qr code in a text area, div or other HTML element and assign it an id
use that id to extract the data via pure JS, now you have a text variable and you have the possibility to make qr code dynamic now
once you have the data dynamically extracted via JS you can use that variable to generate qr codes like this :
var x = 'sample text'
var qr = window.qr = new QRious({
element: document.getElementById('qrious'),
size: 200,
value: x
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrious/4.0.2/qrious.min.js"></script>
<canvas id="qrious">

Fdf to pdf How to check a checkbox on pdf filling

I am using FDF to fill the pdf with php . I have some checkboxes in the pdf .
I searched a lot on interment . I refered these links refer1 link2.
currently i used this
<</T(form1[0].#subform[6].Checkbox1a[0])/V(1)>>
<</T(form1[0].#subform[6].Checkbox1b[0])/V(checked)>>
<</T(form1[0].#subform[6].Checkbox1c[0])/V(yes)>>
<</T(form1[0].#subform[6].Checkbox1d[0])/V(on)>>
I have tried 1, checked, yes , on etc but failed to ckeck the checkbox . this is one of the pdf i am using to fill pdf link
In the PDF specification the V entry of a check box is described as following:
The V entry in the field dictionary [...] holds a name object representing the check box’s appearance state, which shall be used to select the appropriate appearance from the appearance dictionary.
It is also defined that the name for the in state should be Yes but that's not always the case. In your document the on state is defined as Y.
So your FDF data should look like:
<</T(form1[0].#subform[6].Checkbox1a[0])/V/Y>>
For completion: If the export value is non-Latin text (by using an Opt array) e.g. Acrobat doesn't use a name object but a string, too:
<</T(form1[0].#subform[6].Checkbox1a[0])/V(Y)>>
This behaviour seems to be un-documentated.

Submit entire PDF form to a PHP script

For an insurance company, I've created PDF forms of their various insurance applications. These will be available on their website (when it is done). Each form, being in PDF, looks exactly like the printed version of the application. The idea is that a user fills it out online, hits the (visible but unprintable) "Submit" button at the bottom of the form, and the entire completed PDF is sent to the company. That way they can print it out, and carry on with their current paper filing system.
I experimented with using a "mailto:..." URL for the Submit button, but that requires an external mail program. It also gives the attached PDF a random temp filename.
Therefore, I'd like to send the PDF to a php script, change the name, and email it to the company from there. I played with it a bit, and I can reach the destination script, but I have no idea what happens to the PDF. I expected it to show up in $_FILES, but nothing.
Saving to a database is not an option.
When preparing the PDF form, you can choose how you would like to submit: as an HTML form (as Sepodati said), as XML data, as FDF (which only sends data, and will open the original PDF when displaying the data), or as a completed PDF. For ease of use for the site owners, who are not computer savvy, I'm going to send them a completed PDF that they can print.
So, the same question remains: where does my PDF file go when I get to my destination script?
I'm submitting the PDF via a Submit button on the PDF itself. You'll have to be familiar with PDF forms to understand this. For the URL, I put the destination PHP script.
In my destination script, I tried the following
PHP Code:
reset ($_FILES);
while (list ($key, $val) = each ($_FILES)) {
echo "$key => $val<br />\n";
}
I did this for $_FILES, $_GET, $_POST, and $_REQUEST. Nothing came up, as though each array was empty. I might have done this wrong, though.
I put phpinfo() in the script, and these vars appear related to the PDF...
_SERVER["CONTENT_LENGTH"] = 37722
_SERVER["CONTENT_TYPE"] = application/pdf
_SERVER["HTTP_USER_AGENT"] = Mozilla/4.0 (compatible; Adobe Acrobat Control Version 5.00 for ActiveX)
_SERVER["REQUEST_METHOD"] = POST
The content length is approximately the size of the PDF.
Something else I didn't notice before is that upon submission, Internet Explorer displays this page (or a name like it):
C:\WINDOWS\TEMP\pa003773.html
This page contains all the output from my script.
It's very interesting that it's supposed to send the entire PDF. I would be interested in knowing how it does that because I've searched the Adobe site for information on how to submit an entire PDF file by clicking a button on the PDF form.
The only thing I found on their site was information about the FDF and XFDF formats. Both of these formats have to be parsed in some way by your form handler script and aren't included in $_FILES the way a file is uploaded when you select the file from the filesystem.
$fptr = fopen("temp.fdf", "w");
fputs($fptr, $HTTP_RAW_POST_DATA);
fclose($fptr);
This is a really old post so I'm sure you've gotten your answer by now but here's what worked for me in case anyone else stumbles across this in the future.
BTW- this only works for if you are submitting the entire PDF with the form fields completed.The address you would put on your submit button would be something like http://youserver.com/lib/php/formProcess.php?email=to#company.com&formName=Asset%20Inventory
<?php
ob_start();
$file = file_get_contents("php://input"); //Gets binary PDF Data
$time = microtime(true);
//Names pdf file based on the time to the microsecond so nothing gets overwritten. Obviously you can change this to whatever you want or do something like $_REQUEST['formName'] and just include the form name in your URL from your pdf submit button
$newfile = "forms/" . $time . ".pdf";
$worked = file_put_contents($newfile, $file); //Creates File
ob_end_clean();
//Upon completion you can either return XFDF to update a field on your form or display a PDF document. I chose the PDF route because it worked for our situation.
$successFile = 'success.pdf';
$errFileFile = 'error.pdf';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSendmail();
$mail->setFrom('From email address', 'from name');
$mail->addReplyTo('Reply to email', 'Reply to name');
$mail->addAddress($_REQUEST['email']); //get email from URL- alternately you can just hardcode in an address
$mail->addAttachment( $newfile );
$mail->isHTML(true);
$mail->Subject = 'New form submission';
$mail->Body = 'A new form has been submitted.';
if(!$mail->send()) {
header('Content-type: application/pdf');
readfile($errFile);
} else {
header('Content-type: application/pdf');
readfile($successFile);
//if you want to delete the file from the server after emailing: unlink($newfile);
}
?>

Populating PDF form fields with PHP

I am converting a classic ASP application to PHP.
In the Classic ASP app, we are programatically populating fields that have been created in PDF documents using a component called ASPpdf.
I need to reproduce this behavior in PHP, but need to know if PHP can populate PDF fields on its own or if any third party plug in is needed.
Is this functionlity posible in PHP with or without a plug in?
Thanks.
Note: I already have the PDFs created, I do not need to create the actual PDF. I need to grab a preexisting PDF with form fields, populate those form fields and then save that custom PDF.
This is the first google search result I got, is there a reason this doesn't work for you?
http://koivi.com/fill-pdf-form-fields/tutorial.php
UPDATE
After reading a little further, this generates an FDF (which Acrobat can read). To generate an actual PDF you'll need to use this: http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
The resolution to my problem was to use the same COM component I used in Classic ASP in my new PHP apps. The COM object give me tremendous control over PDF documents.
The component I use is AspPdf
<?php
$pdf = new COM("Persits.Pdf") or die("Unable to instantiate Word");
$pdfdoc = $pdf->OpenDocument( "pdf.pdf" );
$pdffont = $pdfdoc->Fonts("Helvetica-Bold");
$pdfdoc->Form->RemoveXFA();
$pdffield = $pdfdoc->Form->FindField("FirstName");
$pdffield->SetFieldValue("PHP Text", $pdffont);
$pdffile = $pdfdoc->save( "php.pdf" , false);
echo $pdf->version;
$pdf = null;
?>
It looks as though the pdftk PDF toolkit may be able to do this. Reading the manual:
fill_form < FDF data filename | XFDF data filename | - | PROMPT >
Fills the single input PDF’s form fields with the data from an FDF file, XFDF file or stdin. Enter the data filename after fill_form, or use - to pass the data via stdin, like so:
pdftk form.pdf fill_form data.fdf output form.filled.pdf

Dynamically insert content into pdf files with php

I have an ebook in word that I convert to PDF before distributing to my clients. I'd like to dynamically insert their email address into all links in the ebook to allow them access to the members-only content on my site, and I'd like to do this on the fly, as part of the book download process.
I've briefly looked at http://us.php.net/pdf and FPDF, but I was wondering what specific technique I'd use to insert this data.
I was thinking I'd insert an email token string where I want the email address to go, and then use some function to update those tokens in the PDF document.
Can anyone point me in the right direction? I have php experience, but not with editing / generating pdf documents from php.
EDIT: Yes, this commercial script http://www.setasign.de/products/pdf-php-solutions/setapdf-linkreplacer/ does exactly what I needed.
So far it's looking like this is my best bet:
http://www.setasign.de/products/pdf-php-solutions/setapdf-linkreplacer/
Trying an eval copy of it, will update post with results.
you can do this with FPDI extension for FPDF http://www.setasign.de/products/pdf-php-solutions/fpdi/
it enables fpdf to import existing pdf files, though I'm not sure how can one replace links.
I'd say your best shot would be to generate the whole thing in php, or just save it in html, replace links in html, then convert html to pdf.
Without using the Adobe LiveCycle Designer, the easiest way to generate a custom PDF is to use an FDF file. There are tons of ways to do this, one of which is to download binaries from Adobe and install them on your server. But none of that is really needed. All you need is a pdf with fillable forms and a simple script that makes and FDF file. The FDF simply holds the data that needs to be filled in and a pointer to the pdf file to be filled in. I use this for our timesheets at work. The data goes into a web form, but must come out static and ugly and using a paper from from 30 years ago. Here's what your fdf file will look like (both with code and raw):
$file = "http://www.example.com/blankpdfform.pdf";
$data = "%FDF-1.2\n%âãÏÓ\n1 0 obj\n<< \n/FDF << /Fields [ ";
foreach($datafields as $field => $val) {
$data.='<< /T ('.$field.') /V ('.trim($val).')>> ';
}
$data.="] \n/F (".$file.") /ID [ <".md5(time()).">\n] >>".
" \n>> \nendobj\ntrailer\n".
"<<\n/Root 1 0 R \n\n>>\n%%EOF\n";
The end result being:
%FDF-1.2\n%âãÏÓ\n1 0 obj\n<< \n/FDF << /Fields [<< /T (email) /V (email#address.com)>>
/F ("http://www.example.com/blankpdfform.pdf") /ID [ <"SomeUniqueID">
] >> \nendobj\ntrailer<<
/Root 1 0 R
>>
%%EOF
If you have the PDF template with the email token stored in a file on the webserver, you can do this fairly easily. First you need to read in the file using PHP. You can do this using the file_get_contents method. Then use str_replace to replace the email token with the actual email. Finally, serve the file with the correct content-type.
$pdf = file_get_contents( 'template.pdf' );
$pdf = str_replace( '__EMAIL__TEMPLATE__', $userEmail, $pdf );
header( 'Content-type: application/pdf' );
print $pdf;
Doc links:
http://ca2.php.net/manual/en/function.file-get-contents.php
http://ca2.php.net/manual/en/function.str-replace.php
(I haven't actually tried this and you may run into some issues since PDF is a binary format, but in theory it should work...)

Categories