• Resolved mabee004

    (@mabee004)


    Is there a way to either open the pdf or provide a link to open it, or immediately download it when the user hits submit rather than send it via email?

Viewing 1 replies (of 1 total)
  • Plugin Author maximum.software

    (@maximumsoftware)

    I don’t think this would fit with Contact Form 7 integration unless you can find a ‘download attachment on form submission’ type plugin. However, I have plans to write another plugin that would do what you are describing.

    For now, what you can do is the following,

    1. Create a button that would run a JavaScript function.
    2. Create a JavaScript function that would take your form data and submit it to /pdf-fill.php as a POST request with array ‘data’ argument where the keys would match the field names in the PDF form
    3. Create php-fill.php in your public_html with the following code,

    
    <?php
    
    define('WP_USE_THEMES', false);
    require(dirname(__FILE__).'/wp-load.php');
    
    $data = $_POST['data'];
    
    // change this when using a different PDF file
    $attachment_id = 123;
    
    try
    {
    	if( !class_exists( 'WPCF7_Pdf_Forms' ) )
    		throw new Exception(__("WPCF7_Pdf_Forms not loaded"));
    	
    	$tempfile = tempnam(get_temp_dir(), "pdf_");
    	if(!$tempfile)
    		throw new Exception(__("Failed to create a temporary file"));
    	
    	$service = WPCF7_Pdf_Forms::get_instance()->get_service();
    	if(!$service)
    		throw new Exception(__("Failed to get PDF forms filling service"));
    	
    	$service->api_fill( $tempfile, $attachment_id, $data );
    	
    	$file = fopen($tempfile,'rb');
    	if(!$file)
    		throw new Exception(__("Failed to read a temporary file"));
    	
    	header("Content-Type: application/pdf");
    	header("Content-Length: " . filesize($tempfile));
    	
    	fpassthru($file);
    	fclose($file);
    	@unlink($tempfile);
    }
    catch(Exception $e)
    {
    	@unlink($tempfile);
    	header($_SERVER['SERVER_PROTOCOL']." 500 Internal Server Error", true, 500);
    	print $e->getMessage();
    }
    

    This should do what you are looking for.

Viewing 1 replies (of 1 total)
  • The topic ‘Is there a way to post PDF rather than send?’ is closed to new replies.