Attach to email a PDF generated by FPDF script

I’m using FPDF to generate PDFs from data and it’s working perfectly. I now want to send a generated PDF as an attachment in an email.

What’s the method for doing this? The PDF is an invoice so I’ve tried putting this in the attachment:

/invoice.php?output=email&inv={{InvoiceID}}

but it’s giving me this 500 error:

message: "fopen(/home/public_html/invoice.php?output=email&inv=5): failed to open stream: No such file or directory"

The PHP script is definitely in that location so I’m thinking I’m going about this the wrong way.

Is anyone able to offer some help?

hi @sitestreet

How did you use FPDF with wappler. Can you share an example?

Hi @s.alpaslan. I wrote the PHP in the normal way as shown by FPDF and then just linked to it directly. So Wappler wasn’t actually used for that particular page. I added a simple check for the session variable so it only ran when a user is logged in like this:

session_start();
if($_SESSION['securityId'] > 0) {

FPDF CODE HERE

}

and a query to get all the data.

2 Likes

Like the same way i have generated the pdf but i don’t know how to send the pdf in email via script. please help me out on this. need some working reference code.

Thanks
Maddy

You save the PDF somewhere in your folder structure and then attach it to the email within the send mail server action.

hi i’m not storing the file in any folder. WIthout saving is the file is that possible to send the generated pdf in mail. i like to send email using smtpjs. Please help me.

You need to store it otherwise there isn’t anything to attach. You can add another action to delete the file again (or completely empty the folder to be absolutely sure nothing is left on there) once the email has been sent.

Since you are using a php page to process the download does it look something like this?

Example – have to set a temporary path to save output as a file on your server FIRST as sitestreet is explaining.

$path = "/absolute_path_to_your_files/";

https://gist.github.com/finalwebsites/be5f1dc1dd65f2bd8645e0495fb51896

Maybe THIS more directly addresses your problem:

`Output
string Output([string dest [, string name [, boolean isUTF8]]])
Description
Send the document to a given destination: browser, file or string. In the case of a browser, the PDF viewer may be used or a download may be forced.
The method first calls Close() if necessary to terminate the document.
Parameters

dest
Destination where to send the document. It can be one of the following:

    I: send the file inline to the browser. The PDF viewer is used if available.
    D: send to the browser and force a file download with the name given by name.
    F: save to a local file with the name given by name (may include a path).
    S: return the document as a string.

The default value is I. 

name
The name of the file. It is ignored in case of destination S.
The default value is doc.pdf.
isUTF8
Indicates if name is encoded in ISO-8859-1 (false) or UTF-8 (true). Only used for destinations I and D.
The default value is false. `

Hi @sitestreet and/or @Maddy_Mathan,

Can I ask you how you got FPDF going? I’d be super happy if you could share some details.
I’m trying to get FPDF playing together nicely, with Wappler. (I’ve used a ported version with Python before). However, things aren’t working as I expected.

What I want is to generate a pdf for each row returned from a database query. So let’s say I have a query that returns rows with a column ‘name’. I want to create a pdf called name.pdf with that name displayed in the pdf, for each row. (and eventually also email this as an attachment, just like @Maddy_Mathan.

What I tried, so far, is to put the whole FDPF (fpdf183 in my case) folder in dmxConnect/modules/lib, so I can acces it as a server action in the Library:

I then do the query (a paged query with a limit of 2 for now). Create a repeat. Set session value like so (so I can dynamically access the name in the fpdf php file):

And as a last step I add an include action and select the fpdf php file, like so:

Here is an example of the fpdf php file:


fpdf_wappler_test_101.zip (899 Bytes)

To me that looked pretty solid. And it does in fact create one pdf when I run the server action in a browser. However the browser returns this error:

And just one pdf, not multiple:

Also, the editor of the server action gets a bit corrupted and the steps within the repeat disappear, but I’ve already filed this as a bug.

Am I even remotely close to your solutions, or did you incorporate fpdf totally differently?
I would be super grateful for a response.

bg

Jelle
(a Bubble user taking the leap to Wappler because of scalability issues)

@jellederijke this comment is for you dear.

Working example code :wink:

$name= $POST[‘name’] ; // this data collect from php form as <input
$pdf->Cell(47,10,“name”,1,0); // this you have to place in pdf.php (that you use to convert the data to pdf)
$pdf->Output($name. date(“m-d-Y”). ‘.pdf’, ‘I’); //

Result of your file extension : name_date(m-d-y).pdf :wink:

Ping me to my mail id (maddymathan04@gmail.com) for move will take remote to explain.

Thanks

1 Like

Sending in mail i need some more clarity could you email pls. maddymathan04@gmail.com

otherway round trying to get the above in my code.

Thanks

I didn’t bring FPDF into Wappler at all. I created a completely standalone PHP script and simply saved it amongst all the other scripts and called it using an API Action and send variables through the URL. The PHP file included the security check:

and I then used the File Remove action to delete the file again.

1 Like

I had a number of comments regarding whether php pdf process file had a white space before the ?<php and on the Windows file system and “cleaning up” diacritics in names with umlauts to a

CONVERT TO CHARACTER SET utf8

etc.
But will wait for further developments here. :nerd_face:

Now I see something else in your code, because you are saving a pdf file in a hosting account folder whose name may contain UMLAUTS then the hosting server cannot find the pdf file because the name of that particular file is saved with a “corrupted” name that Windows finds incompatible in format.

Just as you folder view shows you.

Try cleaning up the file name to UTF8 & whatever localization translations your hosting company is friendly to and understands.

PHP functions and Wappler itself can give a last name (used for file.pdf) such as Schröder a universally recognized value of schroeder.pdf. Then it can be found in the folder in all hosting server environments.

Your php pdf process should be able to find that pdf in its folder and not report it missing.

1 Like

I feel so lucky getting these kind responses, @Maddy_Mathan, @sitestreet, @NewMedia!

Thanks :grin: :grin: :grin:

After some fiddling with the content of @Maddy_Mathan’s comment and googling some php-basics (that I am clearly lacking) I got it going, thanks guys!

So for fellow Bubble user getting into Wappler or other Wappler users that could categorize themselves as novice; here is a small writeup of how to batch create pdfs using FPDF with Wappler:

1: Download FPDF for php (in this case) and copy complete folder to your wappler project folder. Create a new pdf folder within that folder that will contain your code to create the pdf (more on this in later step).

2: Go to server actions, add a new action and Setup a Query that returns the data you want, as a first step. I suggest to start with a paged query so you can easily control the number of rows returned with the LIMIT param.

3: add a repeat with an API action as a step. Use POST as method, data type FORM and add the data you want to use or display for your pdf as Input Data. It took me a while to figure out how to point to the actual php file. In my case I am running a project in docker on a localhost. So the url would be: http://localhost/fpdf183/filename.php (insert your filename) Here is a screenshot with some example:

4: Edit your php file in your fpdf folder (I use Visual Studio Code) and paste this code to get a working minimum:

<?php
// Get variables from form post
$name= isset($_POST['name']) ? $_POST['name'] : "";
// you can use this next line to check if the variable gets through:
//echo "naam: ".$name;
require('/var/www/html/php_libs/fpdf183/fpdf.php');

//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm

$pdf = new FPDF('P','mm','A4');

$pdf->AddPage();

$pdf->SetFont('Helvetica','',12);

$pdf->Cell(47,10,$name,1,0);
$pdf->Output('../'.$name. '.pdf', 'F');
?>

Save file
5: right click your server action file in Wappler and click run in browser.

6:
Voila your pdf’s will be generated in your wappler project folder!

Thanks again guys.

@NewMedia I will surely get into file name part now, like you suggested. For now I’m super stoked that the pdf’s actually generate!

3 Likes

Beautiful!
I’m Bookmarking this!
Thanks for following through on this path & not only living to tell the tale, but reporting back your findings step-by-step!

1 Like

I can thank most, if not all of my successes to either google searches or help from others, in another way. I feel obligated to add some of my learnings to this community for other answer seekers to find!

So regarding the filenames not being compatible with Windows and special characters such as umlaut. Did you create a modified version of the text.php file like described in this thread?

This seems fairly doable. I’m going to try it right now.

EDIT: so far the custom slugify function from mentioned thread seems to do the job. Super happy!

A small update for everyone that wants to create pdf’s in wappler from a database query (using php backend):

How to handle special characters
I struggled with special characters not being displayed properly, in my pdf’s. Stuff like - or accents etc. Turns out there is another version of fpdf called tfpdf that utilizes UTF-8 fonts. You can download it here: http://www.fpdf.org/en/script/script92.php
It only takes some minor adjustments in your php file and a valid utf-8 ttf file of the font you want to use, et voila!

sending an email with your generated pdf as an attachment
You can send your freshly generated pdf as an attachment quite easily, using the mailer module. I use Sendgrid with smtp. You just setup the mailer like you normally would and then point the attachment towards the path of your generated pdf file. It just works!

For some reason I thought that I’d have to base64 encode the pdf file, before I can send it as an attachment. But apparently that is not necessary.

Speed of smtp
Not super happy with the speed of the smtp method. It takes a long while to proces the records and thus my script times out after a while.

I created some logging to check which records have processed or not. If anyone is interested in how I setup the logging I’m happy to share that.

question regarding sending attachments through sendgrid api
Does anyone have experience sending pdf attachments using the sendgrid api? Do you need to base64 encode your files, and if so how do you handle this with Wappler?

Happy coding/fiddling!