0

I am unable to generate a PDF from a DIV for emailing to a client while maintaining formatting and variables changed using Regex. Essentially, the PDF file sends as it should attached, but the formatting is messed up (i.e., break space, etc.) and the variables are showing up as {input1}, {input2}, etc, even after being generated.

I've tried multiple other answers on the site I found, including CakePHP and it doesn't work for me.

indexing.html: For some reason, I can't post HTML code. Basically it's just a Div with the id 'string1' with some text in between which includes {input1}.

*I used regex to replace {input1} with user input.

pdf.php:

require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

class Pdf extends Dompdf{

public function __construct(){
    parent::__construct();
}
}

?>

PHP:

$message = '';

$html= file_get_contents("indexing.html");
$dom = new DOMDocument;
$dom->loadHTML($html);
$div = $dom->getElementById('string1');
$result = $dom->saveHTML($div);

if(isset($_POST["action"]))
{
include('pdf.php');
$file_name = md5(rand()) . '.pdf';
$html_code = '<link rel="stylesheet" href="bootstrap.min.css">';
$pdf = new Pdf();
$pdf->load_html($result);
$pdf->render();
$file = $pdf->output();
file_put_contents($file_name, $file);

require 'class/class.phpmailer.php';
$mail = new PHPMailer();
#$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host       = ''; // Specify main and backup SMTP servers
#$mail->SMTPAuth   = true; // Enable SMTP authentication
$mail->Username   = ''; // SMTP username
$mail->Password   = ''; // SMTP password
#$mail->SMTPSecure = 'SSL'; // Enable SSL
$mail->Port       = 587; // TCP port to connect to
$mail->setFrom("", "");
$mail->addAddress("");
$mail->isHTML(true);
$mail->AddAttachment($file_name); //Adds an attachment from a path on the filesystem
$mail->Subject = 'Generate Legal Document'; //Sets the Subject of the message
$mail->Body = 'Please see attached PDF File.'; //An HTML or plain text message body
if($mail->Send())   //Send an Email. 
Return true on success or false on error
{
$message = '<label class="text-success">Sent successfully... 
</label>';
}
unlink($file_name);
header("Location: indexing.html");
}

The PDF file sends as it should attached, but the formatting is messed up (i.e., break space, etc.) and the variables are showing up as {input1}, {input2}, etc, even after being generated...

  • Did you write the included `pdf.php` yourself, or have you downloaded it somewhere? I think that's where the problem is? The emailing seems irrelevant to your question. Have you read these instructions: [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – KIKO Software Jun 22 '19 at 20:26
  • Is the `indexing.html` processed in any way? Templating software? PHP? Anything? You have to use the http(s) protocol, otherwise you're just loading the source code. – KIKO Software Jun 22 '19 at 20:37
  • indexing.html is processed. It targets the
    element with the id 'string1'. The script above emails the contents of the
    to a designated email address. My problem is that the formatting is all screwed up - the white spaces aren't there and there are no break spaces in the document.
    –  Jun 24 '19 at 12:31
  • You haven't added anything that wasn't already in your question. My questions were: 1. What is in `pdf.php`? 2. What is in `indexing.html`? Since the likely cause of your problem is in one of these files, and you haven't given us any information about them, we cannot answer your question. – KIKO Software Jun 24 '19 at 12:46
  • I'm sorry, @KIKOSoftware. I'm a noob. I added some information that I hope helps, if not, I'm sorry. I'm doing the best I can :-\ –  Jun 24 '19 at 18:13
  • Thanks. You're using [Dompdf](https://github.com/dompdf/dompdf). That's one thing cleared up. And your converting `indexing.html` to PDF, what's in that one? – KIKO Software Jun 24 '19 at 18:24
  • Thanks! Here's the complete indexing.html file: https://anthonyumbriac.com/pdfsend/indexing.html –  Jun 24 '19 at 21:12
  • @KIKOSoftware, also here is the .pdf file that gets generated and doesn't look right at all: https://anthonyumbriac.com/pdfsend/example.pdf –  Jun 24 '19 at 21:18
  • In your PHP code, instead of using `file_get_contents("indexing.html")`, which loads the source code of the HTML file, try `file_get_contents("https://anthonyumbriac.com/pdfsend/indexing.html")`. The idea is that any processing done by the webserver is **not** bypassed this way. Note that the HTTPS protocol can be problematic, for a possible solution [see this question](https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https). The one using CURL should work in most cases. – KIKO Software Jun 24 '19 at 22:08
  • Cool. I will try those. Thank you very much!!! –  Jun 24 '19 at 22:16

0 Answers0