require('fpdf.php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','',10); $text = "Hello, World!"; $width = $pdf->GetStringWidth($text); $pdf->Cell($width, 10, $text); $pdf->Output();
require_once('tcpdf/tcpdf.php'); $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); $pdf->AddPage(); $pdf->SetFont('helvetica', '', 12); $text = "This is a long text string"; $width = $pdf->GetStringWidth($text); $pdf->Cell($width, 10, $text); $pdf->Output();In both examples, we are using the PDFGetStringWidth function to find the width of the string and dynamically adjust the width of the content in the PDF document. The FPDF library is used in the first example, while the second example is using the TCPDF library.