0

最初は英語で失礼したいのですが、問題があります

この方法でデータベースからテーブルを作成します。

//the connection goes right, so I don't put it into my code

<table id="database">
<tr>
    <th>Title1</th>
    <th>Title2</th>
    <th>Title3</th>
    <th>Title4</th>
</tr>
<?php
    $result = mysql_query("SELECT * FROM `table`");
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>".$row['Column1']."</td>";
            echo "<td>".$row['Column2']."</td>";
            echo "<td>".$row['Column3']."</td>";
            echo "<td>".$row['Column4']."</td>";
        echo "</tr>";
    }
?>
</table>

問題は、テーブルをオンラインで削除したいので、これらのデータをファイルに入れようとしているということです。これらの列をコンピューターのローカルに保存する理由があります。

私はすでに試してみましTCPDFhtml2pdf

これらを に入れる義務はありませんpdfが、保存したいだけです。

あなたが私を助けてくれることを願っています。

更新:与えられた2つの良い解決策があります。しかし、私は自分に問題があったAcrobat Readerので、これを選びました:

<?php
    ob_start();
?>
<table id="database">
    <tr>
        <th>Title1</th>
        <th>Title2</th>
        <th>Title3</th>
        <th>Title4</th>
    </tr>
<?php
    $result = mysql_query("SELECT * FROM `table`");
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>".$row['Column1']."</td>";
            echo "<td>".$row['Column2']."</td>";
            echo "<td>".$row['Column3']."</td>";
            echo "<td>".$row['Column4']."</td>";
        echo "</tr>";
    }
?>
</table>
<?php
    $output = ob_get_contents();
    $filename = "column".time().".xls";
    file_put_contents($filename, $output) or die("can't write to the file");
?>
<a href="<?php echo $filename; ?>">Print</a>
4

2 に答える 2

1

簡単な PDF については、mPDF を強くお勧めします。

http://www.mpdf1.com/mpdf/index.php

  1. mPDFをダウンロード

  2. ローカルサーバーで解凍します

  3. コードに基づく実際の例を次に示します。


<?
//path to your newly installed mPDF
include('/mpdf/mpdf.php');

$html='
<table id="database">
<tr>
    <th>Title1</th>
    <th>Title2</th>
    <th>Title3</th>
    <th>Title4</th>
</tr>';

//$result = mysql_query("SELECT * FROM `table`");
//here just a test
$rows=array();
$rows['Column1']='A';
$rows['Column2']='B';
$rows['Column3']='C';
$rows['Column4']='D';

//while($row = mysql_fetch_array($result))  {
//here just a test
foreach ($rows as $row)  {
    $html.="<tr>";
    $html.="<td>".$row['Column1']."</td>";
    $html.="<td>".$row['Column2']."</td>";
    $html.="<td>".$row['Column3']."</td>";
    $html.="<td>".$row['Column4']."</td>";
    $html.="</tr>";
}
$html.='</table>';

$mpdf=new mPDF('c');
$mpdf->debug = true;
$mpdf->WriteHTML($html);
$mpdf->Output('test','D');
?>

このスクリプトを呼び出すとtest.php、ダウンロード パスに名前が保存された素敵な PDF が生成されます。


ここに画像の説明を入力

于 2013-09-10T11:47:11.710 に答える
1

以下のコードを使用して HTML テーブルとして保存するか、SQL からデータをエクスポートしてローカル SQL サーバーにインポートすることができます。

<?php
ob_start();
?>
<table id="database">
<tr>
    <th>Title1</th>
    <th>Title2</th>
    <th>Title3</th>
    <th>Title4</th>
</tr>
<?php
    $result = mysql_query("SELECT * FROM `table`");
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>".$row['Column1']."</td>";
            echo "<td>".$row['Column2']."</td>";
            echo "<td>".$row['Column3']."</td>";
            echo "<td>".$row['Column4']."</td>";
        echo "</tr>";
    }
?>
</table>
<?php
$output = ob_get_contents();
$filename = "column".time().".txt";
file_put_contents($filename, $output) or die("can't write to the file");
?>
于 2013-09-10T11:37:27.787 に答える