You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TCPDF中设置仅屏幕可见、打印时不显示的链接

Hey, I’ve tackled this exact scenario with TCPDF before—here’s a straightforward way to create a link that’s only visible when viewing the PDF on screen, and gets hidden during printing:

TCPDF supports Optional Content Groups (OCGs) (also called layers), which let you control content visibility for different contexts like screen viewing vs printing. Here’s how to use them for your link:

Step 1: Create a Screen-Only OCG

First, initialize your TCPDF instance, then create an OCG configured to be visible on screen but hidden during printing:

// Initialize TCPDF (adjust parameters to match your setup)
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// Create an Optional Content Group for screen-only content
// Parameters: OCG name, visible on screen, visible on print, exportable
$screenOnlyOCG = $pdf->createOCG('ScreenOnlyContent', true, false, false);

Wrap your link code within the OCG block so it inherits the visibility rules:

// Start the screen-only OCG
$pdf->startOCG($screenOnlyOCG, true);

// Add your link here (customize text, font, position, and target URL as needed)
$pdf->SetFont('helvetica', '', 12);
$targetLink = $pdf->AddLink();
$pdf->SetLink($targetLink, 0, 'https://your-target-url.com'); // Replace with your URL

// Add clickable text (you can also use Image() for image links)
$pdf->Cell(0, 10, 'Click here to access our online resource', 0, 1, 'L', false, $targetLink);

// End the OCG to close the screen-only block
$pdf->endOCG();

Key Notes

  • Testing: Make sure to test in a modern PDF reader (like Adobe Acrobat or Foxit Reader) to confirm the link shows up on screen but disappears in print preview/actual prints.
  • OCG Parameters: The createOCG method’s second argument sets screen visibility (true = visible), third sets print visibility (false = hidden).
  • Flexibility: You can add any content (not just links) inside this OCG if you need other screen-only elements.

内容的提问来源于stack exchange,提问作者Muhammad Asad Farooq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:15:41