Rendering Bootstrap Styles in DomPDF
The fundamental issue you are facing stems from how DOMPDF operates under the hood. It is a pure PHP library that renders PDFs by parsing HTML and CSS, but it does not natively understand or load external web libraries like Bootstrap. When you pass a string containing Bootstrap grid classes to load_html(), DOMPDF sees raw HTML tags (like <div class="col-md-6">) and ignores the associated CSS because that CSS is loaded dynamically by the browser via the Bootstrap CDN link. Since PDF rendering happens in isolation from your web server's HTTP requests, the <link> tag referencing the Bootstrap stylesheet is effectively ignored during the conversion process.
To achieve the desired result where a client downloads a file that looks like your web view, you cannot rely on DOMPDF fetching external resources. Instead, you must inline the necessary CSS directly into the HTML string before passing it to the renderer.
Inlining Bootstrap CSS
The most direct approach is to grab the full content of the Bootstrap CSS file and inject it into your HTML string. If you are using a specific version of Bootstrap, ensure you copy the exact content from the bootstrap.min.css file corresponding to that version.
Here is how you can modify your conversion logic:
// Load your target HTML content
$pr_dt = $_POST['hid'];
// Read the Bootstrap CSS content (ensure path points to the minified CSS used in production)
// This prevents DOMPDF from trying to fetch external CDNs
$bootstrap_css_file = "../assets/css/bootstrap.min.css";
$content = file_get_contents($bootstrap_css_file);
// Combine the CSS and your HTML
$html_to_convert = "<style>" . $content . "</style>" . $pr_dt;
include_once("../assets/converter/dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html_to_convert);
$dompdf->render();
$dompdf->stream("MRA.pdf");
This method works because DOMPDF respects <style> blocks embedded directly in the HTML string. By prepending the CSS, you are essentially creating a self-contained document that includes all necessary styling rules without relying on external network calls.
Using Dompdf\Options for Better Control
While the file inclusion method is functional, managing large CSS strings can become unwieldy if you need to mix other styles or handle specific overrides. A more robust pattern involves using DOMPDF's options object to set the defaultFont, font size, and potentially load external CSS files if your environment permits safe execution.
However, for Bootstrap specifically, keeping the CSS inline is often safer because Bootstrap relies on a complex cascade of utility classes that might conflict with default PDF fonts or layout engines if not fully scoped. You may also need to wrap your content in a container to ensure margins and padding are respected as intended by the grid system.
$pr_dt = $_POST['hid'];
$bootstrap_css_file = "../assets/css/bootstrap.min.css";
$content = file_get_contents($bootstrap_css_file);
// Wrap content to simulate a standard page layout if needed
$html_with_styles = "<html><head><style>" . $content . "</style></head><body>";
$html_with_styles .= $pr_dt;
$html_with_styles .= "</body></html>";
$dompdf = new DOMPDF();
$dompdf->load_html($html_with_styles);
// Configure layout parameters if needed
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->set_option('isRemoteEnabled', false); // Critical for security and offline rendering
$dompdf->render();
$dompdf->stream("MRA.pdf");
Alternative: Server-Side Rendering
If your requirements allow, the most reliable long-term solution is to render the PDF on the server before sending it. You can use a tool like puppeteer (Node.js) or wkhtmltopdf (command line) which have much better support for rendering modern web technologies including Bootstrap grids.
With Node.js and Puppeteer, you simply serve your HTML page normally, and Puppeteer launches a headless browser to capture the rendered DOM into a PDF. This approach automatically resolves all CSS, handles external CDN links, and respects JavaScript-driven layouts if you are using those features alongside Bootstrap.
# Example using wkhtmltopdf command line tool
wkhtmltohtml your_page.html -xhtml -p margin:10mm --javascript-delay=1000 MRA.pdf
This shifts the complexity of CSS parsing to a specialized engine designed for it, rather than forcing a text-based HTML parser to guess at web layout semantics. If you are already using Laravel or PHP frameworks, consider generating the PDF asynchronously or as part of a background job if the rendering time becomes a bottleneck.