Leveraging AJAX and PHP for generating PDF files

My web application is designed to function in a specific way - the user fills out a form, and then using AJAX, the form data is sent to a PHP file that utilizes xpdf to generate a PDF. The goal is for the generated PDF to be easily downloadable on the HTML page through AJAX.

When I directly open pdf.php in the browser, everything works perfectly. However, I am currently working on establishing a connection between the AJAX call and the PHP file so that the generated PDF can be sent back to AJAX for the user to download by simply clicking a button on the page.

AJAX:

$.ajax({
    url: 'pdf.php',
    type: 'POST',
    data: pdfData
})
.done(function(response){
});

pdf.php:

<?php 
include('fpdf.php');
include('fpdi.php'); 

// initiate FPDI 
$pdf =& new FPDI(); 
// add a page 
$pdf->AddPage();
...
$pdf->Output('address.pdf', 'D'); 
?>

Answer №1

Once your PDF conversion is complete, all the necessary information including file path and details will be generated. Send back an HTML or JSON object to your webpage, and generate a new button that directs users to the converted PDF file.

The returned data will include everything you need to know about the PDF file such as name, path, and more.

Answer №2

In order to successfully retrieve and open a PDF file, you need to follow a two-step process instead of trying to accomplish it with just one AJAX call.

  1. Instead of retrieving the PDF contents directly in your AJAX call, have it return a URL that points to the location of the PDF file, such as
    /path/to/getpdf.php?id=23535&securitykey=142505
  2. Use JavaScript to open this new URL in a separate window or tab
  3. The script getpdf.php should be set up to deliver the PDF data with the proper HTTP headers for Content-Type and Content-Disposition, prompting the user to download the file.

After the user decides to either open or save the file, most browsers will automatically close the new tab.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving rows from a MySQL table that contain a specified BIGINT from an array parameter

I've encountered a problem with mysql while using serverless-mysql in TypeScript. It seems like my query might be incorrect. Here is how I am constructing the query: export default async function ExcuteQuery(query: any, values: any) { try { ...

Struggling to locate images in Three.js

I'm facing a challenge with my Vue Cli app where I am attempting to use Three.js for rendering 3D objects within it. The issue arises when I attempt to load an image to utilize as a texture. let scene = new Three.Scene(); const spaceTexture = new Th ...

Determine the minimum and maximum width of jQuery UI resizable during the "resizestart" event

As a newcomer to Javascript, I am facing challenges navigating my way around. Currently, I am attempting to create a jQuery plugin that will facilitate resizing elements using the jQuery UI resizable plugin. My goal is to implement logic that dynamically ...

Is there a way to set a custom width or make the description responsive?

Is there a way to ensure that the description is responsive and automatically goes to the next line instead of extending horizontally? Although using textField could fix this issue, I also need to adjust the padding and outline. Are there any alternative s ...

invoke scrollToPosition function in React Native

I am trying to execute a function once the scrolling momentum is finished in my react native SectionList. After going through the documentation, I discovered onMomentumScrollEnd. However, the problem is that onMomentumScrollEnd only works when we manually ...

Creating a CSS triangle that smoothly transitions between two different colors

Is it possible to create a triangle in CSS that smoothly transitions between two colors without relying on a hover state? .arrow-down { width: 0; height: 0; border-left: 20px solid transparent; border-right: 20px solid transparent; b ...

Adjust the color of a contenteditable div once the value matches

I currently have a table with some contenteditable divs: <div contenteditable="true" class="change"> This particular JavaScript code is responsible for changing the color of these divs based on their content when the page loads. However, I am now ...

Updating text color with Ajax response value

I need assistance with updating the text color of a sensor value displayed using html/ajax. Currently, the sensor value is being displayed successfully, but I want the text color to change based on the value. Here's an example: if value < 50 then f ...

Node.js CallbackHandler: Simplifying event handling in JavaScript applications

I implemented the following function in a .js file to handle an asynchronous network connection: function requestWatsonDiscovery(queryString) { console.log('Query =', queryString); if (typeof queryString !== 'undefined') { ...

Display/Modify HTML form

Looking for advice on how to create an interactive HTML form that displays data and allows users to edit it by clicking 'Edit' before submitting changes. Any suggestions on how to implement this functionality? ...

Stop the Router from Displaying the Page momentarily prior to Redirecting

Currently, I have set up a session context for my NextJS application where users accessing pages within the /app/ directory are required to undergo an authorization check before being granted access. Although the logic is functioning correctly in redirect ...

Issue with populating JSON data into jQuery Datatable

Upon receiving a JSON response from the Django backend, I am facing difficulty in getting the datatable to read and display it properly. Any suggestions on how to achieve this? [{ "model": "model name", "pk": 2, "fields": { "name1 ...

Update the dropdown field selection to the color #333 with the help of javascript

I am facing an issue with a dropdown field that has placeholder text and options to select. Initially, both the placeholder text and the options were in color #333. However, I managed to change the color of the placeholder text to light grey using the foll ...

Choosing elements in jQuery

Having some trouble with the subnav on my current website project. I think the issue lies in how I am selecting items in my jquery code. It seems like a small fix that needs to be made, but I'm unsure of the correct approach. http://jsfiddle.net/ZDEr ...

Ensure that any requests to www.domain.com are consistently redirected to domain.com

My website is experiencing an unexpected redirect issue where it keeps directing from www.mydomain.com to mydomain.com. Despite being a Wordpress Multi User installation, there is nothing in the .htaccess file that would trigger such redirection. I have t ...

Customizing the placeholder font size in Material UI Autocomplete using ReactJS

Is there a way to change the placeholder font size for Material UI Autocomplete? https://i.stack.imgur.com/x71k2.png <Autocomplete multiple id="tags-outlined" options={top100F ...

Refreshing data in CakePHP through ajax to display an updated table

I'm struggling to locate any references on this topic. My challenge involves a webpage that showcases a list of users. Above the list, I can successfully update the user information using ajax. Once updated, I need the table below to reflect these ch ...

Inaccurate time displayed by PHP

After attempting to run the code provided, the output shows 12:16:35 am instead of the expected time of 12:31. Can anyone offer a solution to correct this issue? The Code: <?php date_default_timezone_set('Europe/Amsterdam' ...

Connect parent-child models with checkboxes

Currently, I am working on an application where I need to link checkboxes for a role-based menu. The challenge lies in dealing with parent-child checkboxes and ensuring that the values of checkboxes are checked based on user input 1, 1.1, 1.2, 2, 3. Despi ...

Elements that smoothly transition in and out of view

I have implemented this effect in a project, and now I want to replicate it at both the top and bottom of the page. For example, I want elements to fade in when scrolling into view at the bottom of the page, and fade out when scrolling out of view. Simila ...