Dompdf watermark issue
November 14, 2024 · AI Conversations
### 1. **Check the Plugin Settings**
If you are using a specific WooCommerce PDF invoice plugin (e.g., **WooCommerce PDF Invoices & Packing Slips**), follow these steps:
– Go to **WordPress Admin Dashboard** → **WooCommerce** → **PDF Invoices** or the respective plugin settings.
– Look for a section related to **watermark** or **invoice customization**.
– Check if there is an option for adding or editing watermark text.
### 2. **Check the Dompdf Code in Your Custom Plugin/Theme**
If you are directly using **Dompdf** in your custom plugin or theme, the watermark might be added programmatically. Check your code in the following files:
– Locate the file where you are generating the PDF (e.g., `invoice.php`, `generate-invoice.php`, etc.).
– Look for the watermark-related code, usually defined with **CSS** or a specific method like:
$dompdf = new Dompdf();
$canvas = $dompdf->getCanvas();
$canvas->page_text(30, 770, "Your Watermark Text", "Helvetica", 12, array(0.85, 0.85, 0.85));
– The `page_text` method adds text to the PDF, which could be used for watermarks.
### 3. **Search for Watermark Code in Your Theme/Plugin**
You can also perform a search across your theme or plugin files:
– Use an editor like **VS Code** or **PHPStorm**.
– Search for keywords like `"Watermark"`, `"page_text"`, or `"canvas"` in your project files.
### 4. **Check for Custom CSS in Dompdf**
Sometimes, watermarks can be added using custom CSS in the HTML template:
<div style="position: fixed; top: 50%; left: 50%; opacity: 0.1; transform: rotate(-45deg); font-size: 50px;">
Your Watermark Text
</div>
Look for similar inline styles or CSS files linked to your PDF generation.
### 5. **Check the `woocommerce_pdf_invoice_` Hooks**
If you are using hooks in WooCommerce, there might be a filter or action hook adding the watermark. Check for custom code like:
add_action('woocommerce_pdf_invoice_before_content', 'add_custom_watermark');
function add_custom_watermark($dompdf) {
$canvas = $dompdf->getCanvas();
$canvas->page_text(30, 770, "Your Watermark", "Helvetica", 12, array(0.85, 0.85, 0.85));
}
### 6. **Inspect the Plugin Documentation**
If you are unsure about the source, consult the documentation of the WooCommerce PDF invoice plugin you are using, as it may have specific instructions on watermark customization.
By following these steps, you should be able to identify and modify the source of the watermark text in your PDF invoices.
### Step-by-Step Guide to Add Watermark in Dompdf
1. **Load the Dompdf library** if you haven't already:
use DompdfDompdf;
use DompdfOptions;
2. **Initialize Dompdf** and set up your PDF content:
// Set options for Dompdf
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
// Initialize Dompdf with options
$dompdf = new Dompdf($options);
// Your HTML content
$html = '
<h1>Invoice</h1>
<p>This is your invoice content.</p>
';
// Load HTML content into Dompdf
$dompdf->loadHtml($html);
// Set paper size and orientation (e.g., 'A4', 'portrait')
$dompdf->setPaper('A4', 'portrait');
// Render the PDF (convert HTML to PDF)
$dompdf->render();
3. **Add the Watermark Text**:
// Get the canvas object from dompdf
$canvas = $dompdf->getCanvas();
// Set the font and size for the watermark
$font = $dompdf->getFontMetrics()->get_font("Helvetica", "bold");
$size = 50;
// Specify the watermark text, position, rotation, and color (RGB array)
$canvas->set_opacity(0.1); // Set opacity (0.0 to 1.0)
$canvas->text(
100, // X coordinate (horizontal)
400, // Y coordinate (vertical)
'CONFIDENTIAL', // Watermark text
$font,
$size,
array(0.85, 0.85, 0.85) // RGB color (gray in this example)
);
Alternatively, you can use `page_text()` with rotation:
// Add rotated watermark text across the page
$canvas->page_text(
150, // X coordinate
500, // Y coordinate
"WATERMARK", // Text
$font,
$size,
array(0.85, 0.85, 0.85), // RGB color
45 // Rotation angle in degrees
);
4. **Output the PDF**:
// Send the generated PDF to the browser
$dompdf->stream("invoice.pdf", ["Attachment" => false]); // Change to true to force download
### Explanation of Key Parameters
– **Coordinates (X, Y)**: Adjust these values to position the watermark on the page.
– **Opacity**: Use `set_opacity()` to make the watermark text translucent.
– **Font and Size**: Customize font style and size as per your requirements.
– **Rotation**: Use the `page_text()` method with a rotation angle to slant the watermark.
This setup will add a **watermark text** (e.g., "CONFIDENTIAL" or "WATERMARK") diagonally or at the specified position on every page of your PDF invoice.