Access a PDF document by using DOMPDF to open a new page

Currently developing a partially dynamic page where I utilize GET functions to customize it and display the date in various places. At the end of this page, I want to include a button that allows visitors to download/open the page as a PDF without the header. Despite integrating DOMPDF, I am struggling to make it work correctly and need assistance. I have attempted solutions from Stackoverflow without success.

Essentially, I need the entire page printed in the PDF but not automatically open on load - only triggerable by the button click. Additionally, the header (specific div) should be excluded. Is this achievable?

<?php
    require_once("dompdf/dompdf_config.inc.php");

    $html =
        '<html><body>'.
        '<p>This is a test for '.
        '<?php echo htmlentities(substr(urldecode($_SERVER["QUERY_STRING"]), 1)); ?> </p>'.
        '<p>Thank you for reading.</p>'.
        '</body></html>';

    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $dompdf->stream("the_inquiry.pdf");
    return true;
    ?>
<html>
<body>
<div class="shouldnotPrintToPDF">
Content.
</div>
<div class="shouldPrintToPDF">
Sensitive content.
<a href="the_inquiry.pdf">Open or save as PDF</a>
</div>
</body>
</html>

This serves as our one-page presentation containing substantial text. Therefore, I refrain from providing all details here. The challenge lies in duplicating the content in both $html = and within the actual HTML tag. Furthermore, the PDF saving option appears immediately upon page load, which is not intended. I also aim to add the echoed htmlentities part to the PDF name. Is this feasible? The PDF displays the contents set in $html = just fine but fails to respond to the link click.

Update: Following your guidance results in an error "The requested URL /inquiry.php&pdf=1 was not found on this server." Despite placing the target page intended for PDF printing at the root level and DOMPDF in /dompdf, the issue persists. Any insights on this?

Update: Upon adjusting the link, all information displays on a separate page as shown below.

 {Information Display Error}
{More Information Display Error}
{Another Display Error causing frustration}
{Endless Errors with no solution}

Any thoughts on what could be causing this misinformation overload?

Breakthrough:

Enabling DOMPDF_DPI triggers the PDF opening action but causes all text to stack on the initial line of the second page, appearing jumbled. Furthermore, the inclusion of ?&pdf=1 in the htmlentities query string during PDF manifestation creates clutter. This undermines the personalization intent for both the page and PDF.

Answer №1

To enable dompdf to interpret CSS @media queries for different media types (such as screen, print, voice, etc.), you can adjust the settings in the configuration file. By default, dompdf reads styles for the "screen" media type, but this can be modified. Refer to the DOMPDF_DEFAULT_MEDIA_TYPE setting in the configuration. Once configured, include an additional parameter in the URL query string to trigger PDF rendering. For example, if your original URL is like the_inquiry.php?name=Joe, the PDF version could be accessed at

the_inquiry.php?name=Joe&pdf=1
.

Your implementation would resemble the following code snippet:

<?php
ob_start();
?>
<html>
<head>
  <style>
    @media print {
      .shouldnotPrintToPDF, .pdflink { display: none; }
    }
  </style>
</head>
<body>
  <div class="shouldnotPrintToPDF">
    Non-sensitive content.
  </div>
  <div class="shouldPrintToPDF">
    Confidential information.
    <a href="<?php echo $_SERVER['PHP_SELF'] , '?' , http_build_query( $_GET ); ?>&amp;pdf=1" class="pdflink">View or download as PDF</a>
  </div>
</body>
</html>
<?php
if ( isset( $_GET['pdf'] ) ) {
  require_once 'dompdf/dompdf_config.inc.php';
  $html = ob_get_clean();
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->render();
  $dompdf->stream("the_inquiry.pdf");
}
?>

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

Troubleshooting: WordPress background image not displaying

WordPress trouble: The background image I added to my style.css isn't showing up on the site. body{ background: url("/wp-content/themes/my_theme/images.png"); } Any advice would be greatly appreciated! ...

Tips for dynamically resizing hover text descriptions in WordPress to accommodate varying lengths of text

Hey there, I've been struggling with making the text fit inside the hover effect of my image box in a WordPress post. Any suggestions on how to resolve this issue? I have provided a link to a screenshot where the problem is visible. Please take a look ...

The functionality of the Bootstrap tabbed pane is not functioning correctly

I am currently in the process of creating a modal tabbed pane in bootstrap following the instructions provided in this guide. You can view the code and functionality on this fiddle. $(document).on("click","#tabs a",function(event) { alert("!!!"); ...

Guide to effectively invoking JavaScript functions using jQuery

<head> <script src="jquery-latest.js" /> <script> function resetValues(){ alert('Inside the resetValues function'); //using hexadecimal routine 1 -> 3 -> 5 -> 8 -> (11 == a) document.getElementB ...

Upon transitioning to Bootstrap 4, the grid columns automatically wrap

I currently have a table-like display created using the bootstrap grid classes. The body rows on this table are filled dynamically with JavaScript. Depending on the application state, the table is either hidden or shown. I have two divs, one with an ID con ...

Ways to determine if a date matches today's date within a component template

I am currently displaying a list of news articles on the webpage and I want to show the word "Today" if the news article's date is equal to today's date. Otherwise, I want to display the full date on the page. Is there a way to compare the news.D ...

Exploring the world of Bootstrap Twitter 3.0 alongside the wonders of Knockout

When utilizing Twitter Bootstrap, the validation classes like has-error or has-warning must be added to the wrapping form-group element to style both the input and its label. However, Knockout-Validation directly adds the class to the input element itself. ...

Align text to the left of a button with Bootstrap 4

Currently, I have two buttons stacked on top of each other. I'm attempting to include some text to the left of the bottom button, but I am encountering two obstacles: The first issue is that it creates a white round-ish shape around my top button w ...

Is the default choice for the dropdown list set to Razor?

I am encountering an issue with a Razor declaration that results in a duplicated element in a drop-down menu. The code in question is as follows: @Html.DropDownList("SelectedRole", ViewBag.RolesEdit as List<SelectListItem>, ViewBag.CurrentUserRole a ...

Flex container has images stretched, but align-self-center cannot be utilized

Looking for a solution to resolve an issue with the following Bootstrap 4 markup: <div class="col-3 d-flex flex-column"> <img class="align-self-center" src="#"/> </div> The problem arises when using align-self-center in a flex conta ...

Randomly assigning a unique color to each div upon loading the page

I would like to create an effect where the background color of an icon changes randomly to match the color of the text. There are four predefined color choices available for this randomization. Here is the current code I am working with: HTML: <div ...

Using jQuery to apply a CSS border to an image within a textarea

Working with TinyIMCE, I am looking to incorporate a border around all images within the textarea whenever a user submits the form. $("form").submit(function() { var content = $("#edit-message1-value").val(); // Afterwards, I want to manipulate the cont ...

Transforming a subnav bar into a dropdown on smaller screens

Hello everyone, I'm new here and seeking some guidance. Please bear with me if I seem a bit lost or naive. I am attempting to replicate the navigation bar from this website: WOHA. While I don't need it to be an exact replica, I have managed to cr ...

Arranging the <td> element with inline styles (email)

I am trying to format an email signature using a table, but I am facing an issue with how it appears on mobile devices. On mobile screens, the text gets compressed and I want the second image to be displayed below the first one along with the text. While ...

Exploring for worth using BeautifulSoup

Can anyone help me extract the name of a person (Alex Key) from the following HTML markup: <div class="link_container"> <a class="follow_card" data-uuid="e47443373cfa93d5341ab809f0700b82" data-type="person" data-name="Alex Key" data-permalink="/ ...

How to toggle between checked and unchecked states using jQuery within AngularJS?

After reloading, the checkbox should maintain its checked or unchecked state. This functionality can be achieved using a directive controller. var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {}, $checkboxes = $("#c ...

View the full desktop version of the website on your mobile device

Which viewport dimensions should be added to the viewport meta tag in order to view a desktop version of a mobile site while browsing? ...

Implement a hover animation for the "sign up" button using React

How can I add an on hover animation to the "sign up" button? I've been looking everywhere for a solution but haven't found anything yet. <div onClick={() => toggleRegister("login")}>Sign In</div> ...

How can I stop the body from scrolling to 100% height when the virtual keyboard is displayed?

My chat app has sticky header and footer elements. To handle the mobile virtual keyboard opening, I adjust the document's height using window.visualViewport.height. For example, if the browser's height is 1000px and the virtual keyboard takes up ...

What's the secret to creating floating content in one go?

Here is the code I am working with: <div class="coupon-price"> <div class=""> <span class="coupon-new-price"> 65.000</span> </div> <div class=""> <span class="coupon-old-price"> 130.000</span> ...