想要把VisualForce Page转换成PDF只需要在<apex:page>加上renderAs="pdf"
<apex:page renderAs="pdf">
然后可以通过一些CSS来控制一些打印时的设置
具体的CSS转换PDF的用法,可以参照这个链接: http://www.antennahouse.com/CSSInfo/CSS-Page-Tutorial-en.pdf
下面这段代码要创建这样的一个PDF
- 使用A4 横版作为Page size
- 有25mm的Margin
- 每一页有页眉和页脚
- 每一个Div自己一页
<apex:page renderAs="pdf" applyBodyTag="false">
<head>
<style>
@page {
size: A4 landscape;
margin: 25mm;
@top-center {
content: "Sample of Print PDF";
}
@bottom-center {
content: "Page " counter(page) " of " counter(pages);
}
}
.page-break {
display:block;
page-break-after:always;
}
body {
font-family: Arial Unicode MS;
}
.companyName {
font: bold 30px;
color: red;
}
</style>
</head>
<body>
<div class="page-break" align="center">
<h1>Page A</h1>
<apex:panelGrid columns="1" width="100%">
<apex:outputText styleClass="companyName" value="My Company"/>
<apex:outputText value="{!NOW()}"/>
</apex:panelGrid>
</div>
<div class="page-break">Page B</div>
<div>Page C</div>
</body>
</apex:page>
- <style>不是一个VF的标签,所以必须放在<Head>里面,否者不会被解析
- applyBodyTag="false" 是来设置是否允许VF在Render这个页面的时候,自动分配body标签,如果允许VF自动设置,分页等一些CSS显示的都会不正确
- <apex:panelGrid>实际上对应的是HTML的Table标签,所以记得要设置宽度和列数,不然的话,无法按照预期的模式来对齐。
效果如下:
另外还有一些在你使用renderAs的时候要注意的事情
- renderAs只支持PDF
- 一些标准的components和一些form标签,如input、buttons,或者一些需要javascript来呈现的标签,并不容易被render,所以慎用
- 不是所有的Page都可以随便的用renderAs,这个只适用于特意为PDF设计和优化过的VF。
- 如果你的内容是通过JS画出来的,PDF不支持
- 如果PDF无法正确的显示双字节的内容,可以试着调整CSS的字体
<apex:page showHeader="false" applyBodyTag="false" renderAs="pdf">
<head>
<style>
body { font-family: 'Arial Unicode MS'; }
</style>
</head>
<body>
これはサンプルページです。<br/>
This is a sample page: API version 28.0
</body>
</apex:page>
- The maximum response size when creating a PDF must be below 15 MB before being rendered as a PDF. This is the standard limit for all Visualforce requests.
- The maximum file size for a generated PDF is 60 MB.
- The maximum total size of all images included in a generated PDF is 30 MB.
- PDF rendering doesn’t support images encoded in the data: URI scheme format.
- Note that the following components do not support double-byte fonts when rendered as a PDF:
- <apex:pageBlock>
- <apex:sectionHeader>
相关URL
http://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_renderas_pdf.htm
http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_additional_render_pdf.htm