Incorporating CSS Styles in Dompdf Library for Codeigniter

Although I am able to generate a PDF file from the Codeigniter Framework using the Dompdf library, the generated PDF does not retain the styling I applied to the page. How can I ensure that CSS is rendered together with the HTML content in the PDF?

The function responsible for creating the PDF looks like this:

function generateReport($id = null){
 $this->load->library('pdf');

    $data['title'] = 'Student Report';
    $details = $this->student_model->get_single_student($id);
    $result = $this->student_model->get_single_student_result($id);
    $data['student'] = $details;
    $data['result'] = $result;
    $filename = $data['student']['admission_no'];
  $this->pdf->load_view('admin/mark/singleMarkShow', $data);
  $this->pdf->render();
  $this->pdf->stream($filename.".pdf");
 }

Here is an example of the PDF generated:

see PDF here

And this is the HTML page I created to export as a PDF:

view HTML page here

Answer №1

//You can experiment with this code snippet to generate PDFs.

$pdfGenerator = new Dompdf();
$htmlContent = "<div>This is the content of your PDF...</div>";
$htmlContent .= '<link type="text/css" href="/path/to/pdffile.css" rel="stylesheet" />';
$pdfGenerator->loadHtml($htmlContent);

Answer №2

Having faced challenges with DomPDF myself, I understand how daunting it can be.

Although you haven't shared the code for your view ('admin/mark/singleMarkShow'), if you require CSS styling, there are primarily two options:

1.- You can include a link to the CSS file in the view, similar to how you would in an HTML file (

<link rel="stylesheet" href="file.css">
). However, ensure that you provide a full absolute path to the CSS file instead of a relative one. Your href should look something like FCPATH./assets/css/style.css. It's important to note that using large CSS files like bootstrap may significantly slow down DomPDF (refer to this link for more information).

2.- Alternatively, you can inline the necessary styles directly into the view. Simply include the specific styles you need (to minimize overhead) as you would in any HTML file, for example:

<style>
body {
  font-size: 11px;
}

small {
  font-size: 65%;
}
(...)
</style>

While this approach can lead to faster rendering, it requires updating all views individually if there is a redesign.

Regardless of your choice, remember that domPDF expects absolute paths for everything. Even for images, ensure your src attributes use $_SERVER['DOCUMENT_ROOT'] or FCPATH, followed by the remaining route relative to the main index.php file.

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

Execute a JavaScript transition upon clicking following the display block

Can someone assist me with creating a simple transition from right to left when I click on a button? To better explain, I have set up an example here: https://jsfiddle.net/Waarx/9kjhnwLp/22/ var button1 = document.querySelector('#div1'); butt ...

Text overlapping image causing hover state interference

I'm feeling completely lost right now. My goal is to have the title of each project displayed in the center of the screen when you hover over the respective project images. I've resorted to using jQuery for this because it seems like the most str ...

Disable the spinning animation of the Bootstrap spinner to keep it still and static

I am currently utilizing bootstrap 4 for my project. When a particular event starts, I have successfully managed to make the spinner spin with the code snippet below: <span class='spinner-border spinner-border-sm text-primary' role='statu ...

Having trouble loading styles in Vue after publishing to npm

I'm currently using vue-cli3 for the npm publication of a Vue component. Below are my build scripts: "package-build": "eclint fix && vue-cli-service build --target lib --name @xchat-component/chat-component ./src/index.ts & ...

Is there a way to personalize the appearance of a specific page title in my navigation menu?

I'm currently working on customizing the menu of my WordPress theme to display a different color for the active page name. Although my CSS code works well for all page names except the current one: .navbar-nav li a { font-family: georgia; fo ...

Having trouble adding flexslider before a div element with jQuery

Hey there! I recently got flexslider from woothemes.com. The page structure I'm working with looks something like this: <div class="parentdiv anotherdiv"> <div class="child-div1">some buttons here</div> <div class="child-div2"& ...

CSS background image not displaying in ReactJS with SCSS

Currently, I am working on a React project and I am trying to incorporate a local image from a public folder as the background for a div container with transparent color. However, I am facing an issue where the image is not being displayed. I have success ...

Exploring Bootstrap 5: Utilizing flex columns for children elements with full width and equal height display

https://i.sstatic.net/hpajE.png Is there a way to have elements on the left stack vertically, with equal height and width of the parent element, filling the entire height? I need this layout for three or more elements using Bootstrap 5. This is the HTML ...

"Implementing a consistent body border design on all pages using CSS for print media

Having an issue with my html page layout where I am unable to display a border on both printable pages. My current code only adds the border to the first page. Any suggestions on what I need to change in order to show the border on every printed page? Be ...

Creating two columns with the content in the left column aligned with the padding of the container and the second column extending the full width can easily be

My design features a left-side column aligned with container padding, while the right side column spans full width and touches the scroll. Check out the design below: https://i.sstatic.net/w8iCO.png I'm looking to achieve this design using Bootstrap ...

What impact does zoom have on the width of the image?

For more information, you can check out the code here: http://codepen.io/anon/pen/VvdyQb?editors=010 I'm struggling to comprehend how the viewport width changes when zooming in or out. My media queries are activated based on changes in width measured ...

CSS can create a semi-circle shape using borders and outlines

Attempting to craft a CSS circle that mirrors the image below: ...with just a single div: <div class="myCircle"></div> solely utilizing CSS rules. No SVG, WebGL, DirectX, etc. permitted. My initial approach involved constructing a complete ...

Tips for shrinking the circumference of a circle

Currently, I have a circular div that has been styled using CSS animations. My goal is to keep the size of the circle consistent when it moves to the bottom, but reduce its size when it bounces back to the top. I am uncertain if this can be achieved solely ...

Struggling to extract the values from a KendoDropDown using Selenium

When using the selenium web driver, we are trying to retrieve a set of option values such as Last 30 days, Last 60 days, etc. Attempts have been made to locate these elements using a CSS selector. var timeperiodcss = "span.k-widget.k-dropdown.k-header se ...

In Firefox, right floated elements vanish when utilizing columns

Utilizing the ol element with column-count and column-gap properties to organize the list into 2 columns, each list item contains a span element floated right. However, I am encountering an issue where the span element is not displayed for certain items li ...

Display a scrollbar for the offspring of an element with a flex-grow value of 1

I'm seeking to create a straightforward design consisting of a layered header, main section, footer. My framework of choice is bootstrap 5. The complete layout should occupy all available width and height, with overflow (x and y) set to hidden. Hea ...

Ensure the slider functions on all types of browsers

I found a code snippet for an image slider on codepen and integrated it into my project. However, I encountered some issues trying to center the slider in the browser window with 5% space on each side. I attempted using $(window).width(); to set the width ...

`CSS border issues`

I am currently attempting to create a square consisting of 4 smaller squares inside, but I have been facing challenges with the method I was using. Here is the code snippet: #grandbox { position: absolute; width: 204px; height: 204px; border: so ...

The website is missing the display of Google Maps

The Google map is not displaying on my website page that uses PHP. I need assistance with this issue. Below is the webpage URL and the source code: <section class="meteor-google-map fullwidth block" style="height: 450px; border: solid transparent; bo ...

combining: enhancing the value of the background-image property

Here is a mixin that I would like to modify: .a () {background-image: url(one.png);} I want to create a new class .b which inherits properties from .a, but also includes an additional background image layer, like this: .b { .a; ...